Skip to content

TST: organize tests in test_timeseries #32889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions pandas/tests/series/methods/test_autocorr.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"))
73 changes: 14 additions & 59 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from io import StringIO

import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, DatetimeIndex, Series, date_range, timedelta_range
Expand All @@ -19,33 +18,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
Expand Down Expand Up @@ -79,13 +51,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)
Expand Down Expand Up @@ -123,15 +88,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"))
Expand Down Expand Up @@ -175,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))
Expand All @@ -183,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))
Expand All @@ -201,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)