MCPcopy
hub / github.com/pandas-dev/pandas / value_counts_internal

Function value_counts_internal

pandas/core/algorithms.py:841–932  ·  view source on GitHub ↗
(
    values,
    sort: bool = True,
    ascending: bool = False,
    normalize: bool = False,
    bins=None,
    dropna: bool = True,
)

Source from the content-addressed store, hash-verified

839
840
841def value_counts_internal(
842 values,
843 sort: bool = True,
844 ascending: bool = False,
845 normalize: bool = False,
846 bins=None,
847 dropna: bool = True,
848) -> Series:
849 from pandas import (
850 DatetimeIndex,
851 Index,
852 Series,
853 TimedeltaIndex,
854 )
855
856 index_name = getattr(values, "name", None)
857 name = "proportion" if normalize else "count"
858
859 if bins is not None:
860 from pandas.core.reshape.tile import cut
861
862 if isinstance(values, Series):
863 values = values._values
864
865 try:
866 ii = cut(values, bins, include_lowest=True)
867 except TypeError as err:
868 raise TypeError("bins argument only works with numeric data.") from err
869
870 # count, remove nulls (from the index), and but the bins
871 result = ii.value_counts(dropna=dropna)
872 result.name = name
873 result = result[result.index.notna()]
874 result.index = result.index.astype("interval")
875 result = result.sort_index()
876
877 # if we are dropna and we have NO values
878 if dropna and (result._values == 0).all():
879 result = result.iloc[0:0]
880
881 # normalizing is by len of all (regardless of dropna)
882 normalize_denominator = len(ii)
883
884 else:
885 normalize_denominator = None
886 if is_extension_array_dtype(values):
887 # handle Categorical and sparse,
888 result = Series(values, copy=False)._values.value_counts(dropna=dropna)
889 result.name = name
890 result.index.name = index_name
891
892 elif isinstance(values, ABCMultiIndex):
893 # GH49558
894 levels = list(range(values.nlevels))
895 result = (
896 Series(index=values, name=name)
897 .groupby(level=levels, dropna=dropna)
898 .size()

Callers 1

union_with_duplicatesFunction · 0.85

Calls 15

sort_indexMethod · 0.95
equalsMethod · 0.95
sort_valuesMethod · 0.95
sumMethod · 0.95
cutFunction · 0.90
is_extension_array_dtypeFunction · 0.90
SeriesClass · 0.90
IndexClass · 0.90
_ensure_arraylikeFunction · 0.85
value_counts_arraylikeFunction · 0.85
value_countsMethod · 0.45
notnaMethod · 0.45

Tested by

no test coverage detected