From fb85c428a25b96758b143ae657e161a34c4a48a1 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 21 Mar 2020 09:19:31 -0700 Subject: [PATCH 1/2] misplaced timeseries tests --- pandas/tests/frame/test_to_csv.py | 9 ++++ pandas/tests/series/methods/test_autocorr.py | 30 +++++++++++++ pandas/tests/series/test_constructors.py | 7 +++ pandas/tests/series/test_timeseries.py | 45 -------------------- 4 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 pandas/tests/series/methods/test_autocorr.py diff --git a/pandas/tests/frame/test_to_csv.py b/pandas/tests/frame/test_to_csv.py index a49da7a5ec2fc..a9d9d0ace8701 100644 --- a/pandas/tests/frame/test_to_csv.py +++ b/pandas/tests/frame/test_to_csv.py @@ -1352,3 +1352,12 @@ def test_gz_lineend(self): result = f.read().decode("utf-8") assert result == expected + + def test_to_csv_numpy_16_bug(self): + frame = DataFrame({"a": date_range("1/1/2000", periods=10)}) + + buf = StringIO() + frame.to_csv(buf) + + result = buf.getvalue() + assert "2000-01-01" in result diff --git a/pandas/tests/series/methods/test_autocorr.py b/pandas/tests/series/methods/test_autocorr.py new file mode 100644 index 0000000000000..05e3540a7e702 --- /dev/null +++ b/pandas/tests/series/methods/test_autocorr.py @@ -0,0 +1,30 @@ +import numpy as np + + +class TestAutoCorr: + def test_autocorr(self, datetime_series): + # Just run the function + corr1 = datetime_series.autocorr() + + # Now run it with the lag parameter + corr2 = datetime_series.autocorr(lag=1) + + # corr() with lag needs Series of at least length 2 + if len(datetime_series) <= 2: + assert np.isnan(corr1) + assert np.isnan(corr2) + else: + assert corr1 == corr2 + + # Choose a random lag between 1 and length of Series - 2 + # and compare the result with the Series corr() function + n = 1 + np.random.randint(max(1, len(datetime_series) - 2)) + corr1 = datetime_series.corr(datetime_series.shift(n)) + corr2 = datetime_series.autocorr(lag=n) + + # corr() with lag needs Series of at least length 2 + if len(datetime_series) <= 2: + assert np.isnan(corr1) + assert np.isnan(corr2) + else: + assert corr1 == corr2 diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 46ac430a13394..c9f9a28735465 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1428,3 +1428,10 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture): result = Series([Timestamp("2019", tz=tz)], dtype="datetime64[ns]") expected = Series([Timestamp("2019")]) tm.assert_series_equal(result, expected) + + def test_constructor_datetime64(self): + rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s") + dates = np.asarray(rng) + + series = Series(dates) + assert np.issubdtype(series.dtype, np.dtype("M8[ns]")) diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index 563cfa57c9214..e85242172f376 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -1,5 +1,3 @@ -from io import StringIO - import numpy as np import pandas as pd @@ -19,33 +17,6 @@ def assert_range_equal(left, right): class TestTimeSeries: - def test_autocorr(self, datetime_series): - # Just run the function - corr1 = datetime_series.autocorr() - - # Now run it with the lag parameter - corr2 = datetime_series.autocorr(lag=1) - - # corr() with lag needs Series of at least length 2 - if len(datetime_series) <= 2: - assert np.isnan(corr1) - assert np.isnan(corr2) - else: - assert corr1 == corr2 - - # Choose a random lag between 1 and length of Series - 2 - # and compare the result with the Series corr() function - n = 1 + np.random.randint(max(1, len(datetime_series) - 2)) - corr1 = datetime_series.corr(datetime_series.shift(n)) - corr2 = datetime_series.autocorr(lag=n) - - # corr() with lag needs Series of at least length 2 - if len(datetime_series) <= 2: - assert np.isnan(corr1) - assert np.isnan(corr2) - else: - assert corr1 == corr2 - def test_mpl_compat_hack(self, datetime_series): # This is currently failing because the test was relying on @@ -79,13 +50,6 @@ def test_contiguous_boolean_preserve_freq(self): masked = rng[mask] assert masked.freq is None - def test_series_ctor_datetime64(self): - rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s") - dates = np.asarray(rng) - - series = Series(dates) - assert np.issubdtype(series.dtype, np.dtype("M8[ns]")) - def test_promote_datetime_date(self): rng = date_range("1/1/2000", periods=20) ts = Series(np.random.randn(20), index=rng) @@ -123,15 +87,6 @@ def test_groupby_count_dateparseerror(self): tm.assert_series_equal(result, expected) - def test_to_csv_numpy_16_bug(self): - frame = DataFrame({"a": date_range("1/1/2000", periods=10)}) - - buf = StringIO() - frame.to_csv(buf) - - result = buf.getvalue() - assert "2000-01-01" in result - def test_series_map_box_timedelta(self): # GH 11349 s = Series(timedelta_range("1 day 1 s", periods=5, freq="h")) From b436cc11bd88a322d71dc5478c1e8f84d2699416 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 21 Mar 2020 09:22:33 -0700 Subject: [PATCH 2/2] parametrize --- pandas/tests/series/test_timeseries.py | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py index e85242172f376..b340c9d31669e 100644 --- a/pandas/tests/series/test_timeseries.py +++ b/pandas/tests/series/test_timeseries.py @@ -1,4 +1,5 @@ import numpy as np +import pytest import pandas as pd from pandas import DataFrame, DatetimeIndex, Series, date_range, timedelta_range @@ -130,6 +131,19 @@ def test_view_tz(self): ) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("tz", [None, "US/Central"]) + def test_asarray_object_dt64(self, tz): + ser = pd.Series(pd.date_range("2000", periods=2, tz=tz)) + + with tm.assert_produces_warning(None): + # Future behavior (for tzaware case) with no warning + result = np.asarray(ser, dtype=object) + + expected = np.array( + [pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)] + ) + tm.assert_numpy_array_equal(result, expected) + def test_asarray_tz_naive(self): # This shouldn't produce a warning. ser = pd.Series(pd.date_range("2000", periods=2)) @@ -138,12 +152,6 @@ def test_asarray_tz_naive(self): tm.assert_numpy_array_equal(result, expected) - # optionally, object - result = np.asarray(ser, dtype=object) - - expected = np.array([pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02")]) - tm.assert_numpy_array_equal(result, expected) - def test_asarray_tz_aware(self): tz = "US/Central" ser = pd.Series(pd.date_range("2000", periods=2, tz=tz)) @@ -156,11 +164,3 @@ def test_asarray_tz_aware(self): result = np.asarray(ser, dtype="M8[ns]") tm.assert_numpy_array_equal(result, expected) - - # Future behavior with no warning - expected = np.array( - [pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)] - ) - result = np.asarray(ser, dtype=object) - - tm.assert_numpy_array_equal(result, expected)