From 7139344429b67b8cac94f77b29d45107b433031f Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 3 Nov 2020 14:38:12 -0800 Subject: [PATCH 1/4] TST/REF: collect test_timeseries tests by method --- pandas/tests/frame/methods/test_asfreq.py | 11 +++++ .../tests/indexes/datetimes/test_indexing.py | 7 ++++ .../tests/series/apply/test_series_apply.py | 13 +++++- pandas/tests/series/test_arithmetic.py | 15 +++++++ pandas/tests/series/test_timeseries.py | 41 ------------------- 5 files changed, 45 insertions(+), 42 deletions(-) delete mode 100644 pandas/tests/series/test_timeseries.py diff --git a/pandas/tests/frame/methods/test_asfreq.py b/pandas/tests/frame/methods/test_asfreq.py index cdcd922949bcf..368ce88abe165 100644 --- a/pandas/tests/frame/methods/test_asfreq.py +++ b/pandas/tests/frame/methods/test_asfreq.py @@ -74,3 +74,14 @@ def test_asfreq_fillvalue(self): expected_series = ts.asfreq(freq="1S").fillna(9.0) actual_series = ts.asfreq(freq="1S", fill_value=9.0) tm.assert_series_equal(expected_series, actual_series) + + def test_asfreq_with_date_object_index(self, frame_or_series): + rng = date_range("1/1/2000", periods=20) + ts = frame_or_series(np.random.randn(20), index=rng) + + ts2 = ts.copy() + ts2.index = [x.date() for x in ts2.index] + + result = ts2.asfreq("4H", method="ffill") + expected = ts.asfreq("4H", method="ffill") + tm.assert_equal(result, expected) diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index d4ebb557fd6cd..59269b9b54ddc 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -544,6 +544,13 @@ def test_contains_nonunique(self, vals): class TestGetIndexer: + def test_get_indexer_date_objs(self): + rng = date_range("1/1/2000", periods=20) + + result = rng.get_indexer(rng.map(lambda x: x.date())) + expected = rng.get_indexer(rng) + tm.assert_numpy_array_equal(result, expected) + def test_get_indexer(self): idx = pd.date_range("2000-01-01", periods=3) exp = np.array([0, 1, 2], dtype=np.intp) diff --git a/pandas/tests/series/apply/test_series_apply.py b/pandas/tests/series/apply/test_series_apply.py index 9096d2a1033e5..93431a5c75091 100644 --- a/pandas/tests/series/apply/test_series_apply.py +++ b/pandas/tests/series/apply/test_series_apply.py @@ -5,12 +5,23 @@ import pytest import pandas as pd -from pandas import DataFrame, Index, MultiIndex, Series, isna +from pandas import DataFrame, Index, MultiIndex, Series, isna, timedelta_range import pandas._testing as tm from pandas.core.base import SpecificationError class TestSeriesApply: + def test_series_map_box_timedelta(self): + # GH#11349 + ser = Series(timedelta_range("1 day 1 s", periods=5, freq="h")) + + def f(x): + return x.total_seconds() + + ser.map(f) + ser.apply(f) + DataFrame(ser).applymap(f) + def test_apply(self, datetime_series): with np.errstate(all="ignore"): tm.assert_series_equal( diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 9154c566a3dae..fa8f85178ba9f 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -730,6 +730,21 @@ def test_datetime_understood(self): expected = Series(pd.to_datetime(["2011-12-26", "2011-12-27", "2011-12-28"])) tm.assert_series_equal(result, expected) + def test_align_date_objects_with_datetimeindex(self): + rng = date_range("1/1/2000", periods=20) + ts = Series(np.random.randn(20), index=rng) + + ts_slice = ts[5:] + ts2 = ts_slice.copy() + ts2.index = [x.date() for x in ts2.index] + + result = ts + ts2 + result2 = ts2 + ts + expected = ts + ts[5:] + expected.index = expected.index._with_freq(None) + tm.assert_series_equal(result, expected) + tm.assert_series_equal(result2, expected) + @pytest.mark.parametrize( "names", diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py deleted file mode 100644 index 0769606d18d57..0000000000000 --- a/pandas/tests/series/test_timeseries.py +++ /dev/null @@ -1,41 +0,0 @@ -import numpy as np - -from pandas import DataFrame, Series, date_range, timedelta_range -import pandas._testing as tm - - -class TestTimeSeries: - def test_promote_datetime_date(self): - rng = date_range("1/1/2000", periods=20) - ts = Series(np.random.randn(20), index=rng) - - ts_slice = ts[5:] - ts2 = ts_slice.copy() - ts2.index = [x.date() for x in ts2.index] - - result = ts + ts2 - result2 = ts2 + ts - expected = ts + ts[5:] - expected.index = expected.index._with_freq(None) - tm.assert_series_equal(result, expected) - tm.assert_series_equal(result2, expected) - - # test asfreq - result = ts2.asfreq("4H", method="ffill") - expected = ts[5:].asfreq("4H", method="ffill") - tm.assert_series_equal(result, expected) - - result = rng.get_indexer(ts2.index) - expected = rng.get_indexer(ts_slice.index) - tm.assert_numpy_array_equal(result, expected) - - def test_series_map_box_timedelta(self): - # GH 11349 - s = Series(timedelta_range("1 day 1 s", periods=5, freq="h")) - - def f(x): - return x.total_seconds() - - s.map(f) - s.apply(f) - DataFrame(s).applymap(f) From bf080a1a49aae703cf41f7f771b764b95ddf1cfb Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 3 Nov 2020 14:41:15 -0800 Subject: [PATCH 2/4] misplaced DataFrame.values tst --- pandas/tests/frame/methods/test_values.py | 17 +++++++++++++++- pandas/tests/series/test_period.py | 24 ----------------------- 2 files changed, 16 insertions(+), 25 deletions(-) delete mode 100644 pandas/tests/series/test_period.py diff --git a/pandas/tests/frame/methods/test_values.py b/pandas/tests/frame/methods/test_values.py index 564a659724768..a3d7cd7a6e655 100644 --- a/pandas/tests/frame/methods/test_values.py +++ b/pandas/tests/frame/methods/test_values.py @@ -1,6 +1,6 @@ import numpy as np -from pandas import DataFrame, NaT, Timestamp, date_range +from pandas import DataFrame, NaT, Series, Timestamp, date_range, period_range import pandas._testing as tm @@ -44,6 +44,21 @@ def test_values_duplicates(self): tm.assert_numpy_array_equal(result, expected) + def test_values_casts_period_to_object(self): + series = Series(period_range("2000-01-01", periods=10, freq="D")) + + expected = series.astype("object") + + df = DataFrame({"a": series, "b": np.random.randn(len(series))}) + + result = df.values.squeeze() + assert (result[:, 0] == expected.values).all() + + df = DataFrame({"a": series, "b": ["foo"] * len(series)}) + + result = df.values.squeeze() + assert (result[:, 0] == expected.values).all() + def test_frame_values_with_tz(self): tz = "US/Central" df = DataFrame({"A": date_range("2000", periods=4, tz=tz)}) diff --git a/pandas/tests/series/test_period.py b/pandas/tests/series/test_period.py deleted file mode 100644 index 17dbfa9cf379a..0000000000000 --- a/pandas/tests/series/test_period.py +++ /dev/null @@ -1,24 +0,0 @@ -import numpy as np - -from pandas import DataFrame, Series, period_range - - -class TestSeriesPeriod: - - # --------------------------------------------------------------------- - # NaT support - - def test_intercept_astype_object(self): - series = Series(period_range("2000-01-01", periods=10, freq="D")) - - expected = series.astype("object") - - df = DataFrame({"a": series, "b": np.random.randn(len(series))}) - - result = df.values.squeeze() - assert (result[:, 0] == expected.values).all() - - df = DataFrame({"a": series, "b": ["foo"] * len(series)}) - - result = df.values.squeeze() - assert (result[:, 0] == expected.values).all() From 586d751977e683f10560a4ef023fd03be0f4a93c Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 3 Nov 2020 15:03:06 -0800 Subject: [PATCH 3/4] misplaced dataframe.values test --- pandas/tests/frame/methods/test_values.py | 6 ++++-- pandas/tests/series/test_dtypes.py | 23 +---------------------- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/pandas/tests/frame/methods/test_values.py b/pandas/tests/frame/methods/test_values.py index a3d7cd7a6e655..fb0c5d31f692b 100644 --- a/pandas/tests/frame/methods/test_values.py +++ b/pandas/tests/frame/methods/test_values.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas import DataFrame, NaT, Series, Timestamp, date_range, period_range import pandas._testing as tm @@ -44,8 +45,9 @@ def test_values_duplicates(self): tm.assert_numpy_array_equal(result, expected) - def test_values_casts_period_to_object(self): - series = Series(period_range("2000-01-01", periods=10, freq="D")) + @pytest.mark.parametrize("constructor", [date_range, period_range]) + def test_values_casts_datetimelike_to_object(self, constructor): + series = Series(constructor("2000-01-01", periods=10, freq="D")) expected = series.astype("object") diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index b85a53960b0f6..b31525f14e5bc 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -6,7 +6,7 @@ from pandas.core.dtypes.dtypes import CategoricalDtype import pandas as pd -from pandas import Categorical, DataFrame, Series, date_range +from pandas import Categorical, DataFrame, Series import pandas._testing as tm @@ -148,27 +148,6 @@ def test_astype_empty_constructor_equality(self, dtype): as_type_empty = Series([]).astype(dtype) tm.assert_series_equal(init_empty, as_type_empty) - def test_intercept_astype_object(self): - series = Series(date_range("1/1/2000", periods=10)) - - # This test no longer makes sense, as - # Series is by default already M8[ns]. - expected = series.astype("object") - - df = DataFrame({"a": series, "b": np.random.randn(len(series))}) - exp_dtypes = Series( - [np.dtype("datetime64[ns]"), np.dtype("float64")], index=["a", "b"] - ) - tm.assert_series_equal(df.dtypes, exp_dtypes) - - result = df.values.squeeze() - assert (result[:, 0] == expected.values).all() - - df = DataFrame({"a": series, "b": ["foo"] * len(series)}) - - result = df.values.squeeze() - assert (result[:, 0] == expected.values).all() - def test_series_to_categorical(self): # see gh-16524: test conversion of Series to Categorical series = Series(["a", "b", "c"]) From 03dfc81a571d5f5d468c182d0e99a68eda3cb77f Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 3 Nov 2020 15:14:10 -0800 Subject: [PATCH 4/4] collect test by method --- pandas/tests/series/methods/test_values.py | 20 +++++++++++++++ pandas/tests/series/test_dtypes.py | 29 +++++++--------------- 2 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 pandas/tests/series/methods/test_values.py diff --git a/pandas/tests/series/methods/test_values.py b/pandas/tests/series/methods/test_values.py new file mode 100644 index 0000000000000..e28a714ea656d --- /dev/null +++ b/pandas/tests/series/methods/test_values.py @@ -0,0 +1,20 @@ +import numpy as np +import pytest + +from pandas import IntervalIndex, Series, period_range +import pandas._testing as tm + + +class TestValues: + @pytest.mark.parametrize( + "data", + [ + period_range("2000", periods=4), + IntervalIndex.from_breaks([1, 2, 3, 4]), + ], + ) + def test_values_object_extension_dtypes(self, data): + # https://github.com/pandas-dev/pandas/issues/23995 + result = Series(data).values + expected = np.array(data.astype(object)) + tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index b31525f14e5bc..2fbed92567f71 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -120,18 +120,20 @@ def cmp(a, b): s.astype("object").astype(CategoricalDtype()), roundtrip_expected ) + def test_invalid_conversions(self): # invalid conversion (these are NOT a dtype) + cat = Categorical([f"{i} - {i + 499}" for i in range(0, 10000, 500)]) + ser = Series(np.random.randint(0, 10000, 100)).sort_values() + ser = pd.cut(ser, range(0, 10500, 500), right=False, labels=cat) + msg = ( "dtype '' " "not understood" ) - - for invalid in [ - lambda x: x.astype(Categorical), - lambda x: x.astype("object").astype(Categorical), - ]: - with pytest.raises(TypeError, match=msg): - invalid(s) + with pytest.raises(TypeError, match=msg): + ser.astype(Categorical) + with pytest.raises(TypeError, match=msg): + ser.astype("object").astype(Categorical) @pytest.mark.parametrize("dtype", np.typecodes["All"]) def test_astype_empty_constructor_equality(self, dtype): @@ -157,19 +159,6 @@ def test_series_to_categorical(self): tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "data", - [ - pd.period_range("2000", periods=4), - pd.IntervalIndex.from_breaks([1, 2, 3, 4]), - ], - ) - def test_values_compatibility(self, data): - # https://github.com/pandas-dev/pandas/issues/23995 - result = Series(data).values - expected = np.array(data.astype(object)) - tm.assert_numpy_array_equal(result, expected) - def test_reindex_astype_order_consistency(self): # GH 17444 s = Series([1, 2, 3], index=[2, 0, 1])