/usr/local/lib64/python3.6/site-packages/pandas/tests/series/methods
NameSizeModeActions
__pycache__/-0755rm
test_align.py53590644editdlrm
test_append.py96000644editdlrm
test_argsort.py22490644editdlrm
test_asfreq.py36160644editdlrm
test_asof.py55150644editdlrm
test_astype.py26280644editdlrm
test_at_time.py26290644editdlrm
test_autocorr.py9990644editdlrm
test_between.py11970644editdlrm
test_between_time.py50860644editdlrm
test_clip.py33840644editdlrm
test_combine.py6270644editdlrm
test_combine_first.py34110644editdlrm
test_compare.py37340644editdlrm
test_convert_dtypes.py99140644editdlrm
test_count.py11230644editdlrm
test_cov_corr.py52150644editdlrm
test_describe.py46400644editdlrm
test_diff.py23310644editdlrm
test_drop.py29860644editdlrm
test_droplevel.py6290644editdlrm
test_drop_duplicates.py68500644editdlrm
test_duplicated.py9960644editdlrm
test_equals.py12950644editdlrm
test_explode.py35380644editdlrm
test_fillna.py65130644editdlrm
test_first_and_last.py20420644editdlrm
test_head_tail.py3430644editdlrm
test_interpolate.py277570644editdlrm
test_isin.py34450644editdlrm
test_nlargest.py71210644editdlrm
test_pct_change.py29680644editdlrm
test_quantile.py68100644editdlrm
test_rank.py202870644editdlrm
test_reindex_like.py12450644editdlrm
test_rename.py33780644editdlrm
test_rename_axis.py15030644editdlrm
test_replace.py155590644editdlrm
test_reset_index.py46360644editdlrm
test_round.py19700644editdlrm
test_searchsorted.py21210644editdlrm
test_shift.py125950644editdlrm
test_sort_index.py122770644editdlrm
test_sort_values.py79810644editdlrm
test_to_dict.py7420644editdlrm
test_to_period.py17550644editdlrm
test_to_timestamp.py26160644editdlrm
test_truncate.py55570644editdlrm
test_tz_convert.py10080644editdlrm
test_tz_localize.py31080644editdlrm
test_unstack.py42080644editdlrm
test_update.py42130644editdlrm
test_value_counts.py81360644editdlrm
__init__.py2250644editdlrm
Edit: /usr/local/lib64/python3.6/site-packages/pandas/tests/series/methods/test_value_counts.py (8136B)
import numpy as np import pytest import pandas as pd from pandas import Categorical, CategoricalIndex, Series import pandas._testing as tm class TestSeriesValueCounts: def test_value_counts_datetime(self): # most dtypes are tested in tests/base values = [ pd.Timestamp("2011-01-01 09:00"), pd.Timestamp("2011-01-01 10:00"), pd.Timestamp("2011-01-01 11:00"), pd.Timestamp("2011-01-01 09:00"), pd.Timestamp("2011-01-01 09:00"), pd.Timestamp("2011-01-01 11:00"), ] exp_idx = pd.DatetimeIndex( ["2011-01-01 09:00", "2011-01-01 11:00", "2011-01-01 10:00"] ) exp = pd.Series([3, 2, 1], index=exp_idx, name="xxx") ser = pd.Series(values, name="xxx") tm.assert_series_equal(ser.value_counts(), exp) # check DatetimeIndex outputs the same result idx = pd.DatetimeIndex(values, name="xxx") tm.assert_series_equal(idx.value_counts(), exp) # normalize exp = pd.Series(np.array([3.0, 2.0, 1]) / 6.0, index=exp_idx, name="xxx") tm.assert_series_equal(ser.value_counts(normalize=True), exp) tm.assert_series_equal(idx.value_counts(normalize=True), exp) def test_value_counts_datetime_tz(self): values = [ pd.Timestamp("2011-01-01 09:00", tz="US/Eastern"), pd.Timestamp("2011-01-01 10:00", tz="US/Eastern"), pd.Timestamp("2011-01-01 11:00", tz="US/Eastern"), pd.Timestamp("2011-01-01 09:00", tz="US/Eastern"), pd.Timestamp("2011-01-01 09:00", tz="US/Eastern"), pd.Timestamp("2011-01-01 11:00", tz="US/Eastern"), ] exp_idx = pd.DatetimeIndex( ["2011-01-01 09:00", "2011-01-01 11:00", "2011-01-01 10:00"], tz="US/Eastern", ) exp = pd.Series([3, 2, 1], index=exp_idx, name="xxx") ser = pd.Series(values, name="xxx") tm.assert_series_equal(ser.value_counts(), exp) idx = pd.DatetimeIndex(values, name="xxx") tm.assert_series_equal(idx.value_counts(), exp) exp = pd.Series(np.array([3.0, 2.0, 1]) / 6.0, index=exp_idx, name="xxx") tm.assert_series_equal(ser.value_counts(normalize=True), exp) tm.assert_series_equal(idx.value_counts(normalize=True), exp) def test_value_counts_period(self): values = [ pd.Period("2011-01", freq="M"), pd.Period("2011-02", freq="M"), pd.Period("2011-03", freq="M"), pd.Period("2011-01", freq="M"), pd.Period("2011-01", freq="M"), pd.Period("2011-03", freq="M"), ] exp_idx = pd.PeriodIndex(["2011-01", "2011-03", "2011-02"], freq="M") exp = pd.Series([3, 2, 1], index=exp_idx, name="xxx") ser = pd.Series(values, name="xxx") tm.assert_series_equal(ser.value_counts(), exp) # check DatetimeIndex outputs the same result idx = pd.PeriodIndex(values, name="xxx") tm.assert_series_equal(idx.value_counts(), exp) # normalize exp = pd.Series(np.array([3.0, 2.0, 1]) / 6.0, index=exp_idx, name="xxx") tm.assert_series_equal(ser.value_counts(normalize=True), exp) tm.assert_series_equal(idx.value_counts(normalize=True), exp) def test_value_counts_categorical_ordered(self): # most dtypes are tested in tests/base values = pd.Categorical([1, 2, 3, 1, 1, 3], ordered=True) exp_idx = pd.CategoricalIndex([1, 3, 2], categories=[1, 2, 3], ordered=True) exp = pd.Series([3, 2, 1], index=exp_idx, name="xxx") ser = pd.Series(values, name="xxx") tm.assert_series_equal(ser.value_counts(), exp) # check CategoricalIndex outputs the same result idx = pd.CategoricalIndex(values, name="xxx") tm.assert_series_equal(idx.value_counts(), exp) # normalize exp = pd.Series(np.array([3.0, 2.0, 1]) / 6.0, index=exp_idx, name="xxx") tm.assert_series_equal(ser.value_counts(normalize=True), exp) tm.assert_series_equal(idx.value_counts(normalize=True), exp) def test_value_counts_categorical_not_ordered(self): values = pd.Categorical([1, 2, 3, 1, 1, 3], ordered=False) exp_idx = pd.CategoricalIndex([1, 3, 2], categories=[1, 2, 3], ordered=False) exp = pd.Series([3, 2, 1], index=exp_idx, name="xxx") ser = pd.Series(values, name="xxx") tm.assert_series_equal(ser.value_counts(), exp) # check CategoricalIndex outputs the same result idx = pd.CategoricalIndex(values, name="xxx") tm.assert_series_equal(idx.value_counts(), exp) # normalize exp = pd.Series(np.array([3.0, 2.0, 1]) / 6.0, index=exp_idx, name="xxx") tm.assert_series_equal(ser.value_counts(normalize=True), exp) tm.assert_series_equal(idx.value_counts(normalize=True), exp) def test_value_counts_categorical(self): # GH#12835 cats = Categorical(list("abcccb"), categories=list("cabd")) ser = Series(cats, name="xxx") res = ser.value_counts(sort=False) exp_index = CategoricalIndex(list("cabd"), categories=cats.categories) exp = Series([3, 1, 2, 0], name="xxx", index=exp_index) tm.assert_series_equal(res, exp) res = ser.value_counts(sort=True) exp_index = CategoricalIndex(list("cbad"), categories=cats.categories) exp = Series([3, 2, 1, 0], name="xxx", index=exp_index) tm.assert_series_equal(res, exp) # check object dtype handles the Series.name as the same # (tested in tests/base) ser = Series(["a", "b", "c", "c", "c", "b"], name="xxx") res = ser.value_counts() exp = Series([3, 2, 1], name="xxx", index=["c", "b", "a"]) tm.assert_series_equal(res, exp) def test_value_counts_categorical_with_nan(self): # see GH#9443 # sanity check ser = Series(["a", "b", "a"], dtype="category") exp = Series([2, 1], index=CategoricalIndex(["a", "b"])) res = ser.value_counts(dropna=True) tm.assert_series_equal(res, exp) res = ser.value_counts(dropna=True) tm.assert_series_equal(res, exp) # same Series via two different constructions --> same behaviour series = [ Series(["a", "b", None, "a", None, None], dtype="category"), Series( Categorical(["a", "b", None, "a", None, None], categories=["a", "b"]) ), ] for ser in series: # None is a NaN value, so we exclude its count here exp = Series([2, 1], index=CategoricalIndex(["a", "b"])) res = ser.value_counts(dropna=True) tm.assert_series_equal(res, exp) # we don't exclude the count of None and sort by counts exp = Series([3, 2, 1], index=CategoricalIndex([np.nan, "a", "b"])) res = ser.value_counts(dropna=False) tm.assert_series_equal(res, exp) # When we aren't sorting by counts, and np.nan isn't a # category, it should be last. exp = Series([2, 1, 3], index=CategoricalIndex(["a", "b", np.nan])) res = ser.value_counts(dropna=False, sort=False) tm.assert_series_equal(res, exp) @pytest.mark.parametrize( "ser, dropna, exp", [ ( pd.Series([False, True, True, pd.NA]), False, pd.Series([2, 1, 1], index=[True, False, pd.NA]), ), ( pd.Series([False, True, True, pd.NA]), True, pd.Series([2, 1], index=[True, False]), ), ( pd.Series(range(3), index=[True, False, np.nan]).index, False, pd.Series([1, 1, 1], index=[True, False, pd.NA]), ), ], ) def test_value_counts_bool_with_nan(self, ser, dropna, exp): # GH32146 out = ser.value_counts(dropna=dropna) tm.assert_series_equal(out, exp)