@@ -604,7 +604,19 @@ def value_counts(
604
604
ascending : bool = False ,
605
605
bins = None ,
606
606
dropna : bool = True ,
607
+ * ,
608
+ name : Hashable | None = None ,
607
609
) -> Series :
610
+ if name is None :
611
+ result_name = "proportion" if normalize else "count"
612
+ warnings .warn (
613
+ "In pandas 2.0.0, the name of the resulting Series will be "
614
+ "'count' (or 'proportion' if `normalize=True`). Specify "
615
+ f"`name='{ result_name } '` to silence this warning." ,
616
+ FutureWarning ,
617
+ stacklevel = find_stack_level (),
618
+ )
619
+ name = self .obj .name
608
620
609
621
from pandas .core .reshape .merge import get_join_indexers
610
622
from pandas .core .reshape .tile import cut
@@ -626,6 +638,7 @@ def value_counts(
626
638
sort = sort ,
627
639
ascending = ascending ,
628
640
bins = bins ,
641
+ name = name ,
629
642
)
630
643
ser .index .names = names
631
644
return ser
@@ -741,7 +754,7 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:
741
754
742
755
if is_integer_dtype (out .dtype ):
743
756
out = ensure_int64 (out )
744
- return self .obj ._constructor (out , index = mi , name = self . obj . name )
757
+ return self .obj ._constructor (out , index = mi , name = name )
745
758
746
759
def fillna (
747
760
self ,
@@ -1875,6 +1888,8 @@ def value_counts(
1875
1888
sort : bool = True ,
1876
1889
ascending : bool = False ,
1877
1890
dropna : bool = True ,
1891
+ * ,
1892
+ name : Hashable | None = None ,
1878
1893
) -> DataFrame | Series :
1879
1894
"""
1880
1895
Return a Series or DataFrame containing counts of unique rows.
@@ -1979,6 +1994,16 @@ def value_counts(
1979
1994
3 male low US 0.25
1980
1995
4 male medium FR 0.25
1981
1996
"""
1997
+ if name is None and self .as_index :
1998
+ result_name = "proportion" if normalize else "count"
1999
+ warnings .warn (
2000
+ "In pandas 2.0.0, the name of the resulting Series will be "
2001
+ "'count' (or 'proportion' if `normalize=True`). Specify "
2002
+ f"`name='{ result_name } '` to silence this warning." ,
2003
+ FutureWarning ,
2004
+ stacklevel = find_stack_level (),
2005
+ )
2006
+
1982
2007
if self .axis == 1 :
1983
2008
raise NotImplementedError (
1984
2009
"DataFrameGroupBy.value_counts only handles axis=0"
@@ -1991,8 +2016,11 @@ def value_counts(
1991
2016
grouping .name for grouping in self .grouper .groupings if grouping .in_axis
1992
2017
}
1993
2018
if isinstance (self ._selected_obj , Series ):
1994
- name = self ._selected_obj .name
1995
- keys = [] if name in in_axis_names else [self ._selected_obj ]
2019
+ keys = (
2020
+ []
2021
+ if self ._selected_obj .name in in_axis_names
2022
+ else [self ._selected_obj ]
2023
+ )
1996
2024
else :
1997
2025
unique_cols = set (self ._selected_obj .columns )
1998
2026
if subset is not None :
@@ -2015,8 +2043,8 @@ def value_counts(
2015
2043
keys = [
2016
2044
# Can't use .values because the column label needs to be preserved
2017
2045
self ._selected_obj .iloc [:, idx ]
2018
- for idx , name in enumerate (self ._selected_obj .columns )
2019
- if name not in in_axis_names and name in subsetted
2046
+ for idx , _name in enumerate (self ._selected_obj .columns )
2047
+ if _name not in in_axis_names and _name in subsetted
2020
2048
]
2021
2049
2022
2050
groupings = list (self .grouper .groupings )
@@ -2038,7 +2066,7 @@ def value_counts(
2038
2066
observed = self .observed ,
2039
2067
dropna = self .dropna ,
2040
2068
)
2041
- result_series = cast (Series , gb .size ())
2069
+ result_series = cast (Series , gb .size ()). rename ( name )
2042
2070
2043
2071
# GH-46357 Include non-observed categories
2044
2072
# of non-grouping columns regardless of `observed`
@@ -2082,7 +2110,8 @@ def value_counts(
2082
2110
result = result_series
2083
2111
else :
2084
2112
# Convert to frame
2085
- name = "proportion" if normalize else "count"
2113
+ if name is None :
2114
+ name = "proportion" if normalize else "count"
2086
2115
index = result_series .index
2087
2116
columns = com .fill_missing_names (index .names )
2088
2117
if name in columns :
0 commit comments