Skip to content

Commit 3681157

Browse files
authored
TST: organize tests in test_timeseries (#32889)
1 parent 139228b commit 3681157

File tree

4 files changed

+60
-59
lines changed

4 files changed

+60
-59
lines changed

pandas/tests/frame/test_to_csv.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,3 +1352,12 @@ def test_gz_lineend(self):
13521352
result = f.read().decode("utf-8")
13531353

13541354
assert result == expected
1355+
1356+
def test_to_csv_numpy_16_bug(self):
1357+
frame = DataFrame({"a": date_range("1/1/2000", periods=10)})
1358+
1359+
buf = StringIO()
1360+
frame.to_csv(buf)
1361+
1362+
result = buf.getvalue()
1363+
assert "2000-01-01" in result
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
3+
4+
class TestAutoCorr:
5+
def test_autocorr(self, datetime_series):
6+
# Just run the function
7+
corr1 = datetime_series.autocorr()
8+
9+
# Now run it with the lag parameter
10+
corr2 = datetime_series.autocorr(lag=1)
11+
12+
# corr() with lag needs Series of at least length 2
13+
if len(datetime_series) <= 2:
14+
assert np.isnan(corr1)
15+
assert np.isnan(corr2)
16+
else:
17+
assert corr1 == corr2
18+
19+
# Choose a random lag between 1 and length of Series - 2
20+
# and compare the result with the Series corr() function
21+
n = 1 + np.random.randint(max(1, len(datetime_series) - 2))
22+
corr1 = datetime_series.corr(datetime_series.shift(n))
23+
corr2 = datetime_series.autocorr(lag=n)
24+
25+
# corr() with lag needs Series of at least length 2
26+
if len(datetime_series) <= 2:
27+
assert np.isnan(corr1)
28+
assert np.isnan(corr2)
29+
else:
30+
assert corr1 == corr2

pandas/tests/series/test_constructors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,3 +1428,10 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
14281428
result = Series([Timestamp("2019", tz=tz)], dtype="datetime64[ns]")
14291429
expected = Series([Timestamp("2019")])
14301430
tm.assert_series_equal(result, expected)
1431+
1432+
def test_constructor_datetime64(self):
1433+
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
1434+
dates = np.asarray(rng)
1435+
1436+
series = Series(dates)
1437+
assert np.issubdtype(series.dtype, np.dtype("M8[ns]"))

pandas/tests/series/test_timeseries.py

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from io import StringIO
2-
31
import numpy as np
2+
import pytest
43

54
import pandas as pd
65
from pandas import DataFrame, DatetimeIndex, Series, date_range, timedelta_range
@@ -19,33 +18,6 @@ def assert_range_equal(left, right):
1918

2019

2120
class TestTimeSeries:
22-
def test_autocorr(self, datetime_series):
23-
# Just run the function
24-
corr1 = datetime_series.autocorr()
25-
26-
# Now run it with the lag parameter
27-
corr2 = datetime_series.autocorr(lag=1)
28-
29-
# corr() with lag needs Series of at least length 2
30-
if len(datetime_series) <= 2:
31-
assert np.isnan(corr1)
32-
assert np.isnan(corr2)
33-
else:
34-
assert corr1 == corr2
35-
36-
# Choose a random lag between 1 and length of Series - 2
37-
# and compare the result with the Series corr() function
38-
n = 1 + np.random.randint(max(1, len(datetime_series) - 2))
39-
corr1 = datetime_series.corr(datetime_series.shift(n))
40-
corr2 = datetime_series.autocorr(lag=n)
41-
42-
# corr() with lag needs Series of at least length 2
43-
if len(datetime_series) <= 2:
44-
assert np.isnan(corr1)
45-
assert np.isnan(corr2)
46-
else:
47-
assert corr1 == corr2
48-
4921
def test_mpl_compat_hack(self, datetime_series):
5022

5123
# This is currently failing because the test was relying on
@@ -79,13 +51,6 @@ def test_contiguous_boolean_preserve_freq(self):
7951
masked = rng[mask]
8052
assert masked.freq is None
8153

82-
def test_series_ctor_datetime64(self):
83-
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
84-
dates = np.asarray(rng)
85-
86-
series = Series(dates)
87-
assert np.issubdtype(series.dtype, np.dtype("M8[ns]"))
88-
8954
def test_promote_datetime_date(self):
9055
rng = date_range("1/1/2000", periods=20)
9156
ts = Series(np.random.randn(20), index=rng)
@@ -123,15 +88,6 @@ def test_groupby_count_dateparseerror(self):
12388

12489
tm.assert_series_equal(result, expected)
12590

126-
def test_to_csv_numpy_16_bug(self):
127-
frame = DataFrame({"a": date_range("1/1/2000", periods=10)})
128-
129-
buf = StringIO()
130-
frame.to_csv(buf)
131-
132-
result = buf.getvalue()
133-
assert "2000-01-01" in result
134-
13591
def test_series_map_box_timedelta(self):
13692
# GH 11349
13793
s = Series(timedelta_range("1 day 1 s", periods=5, freq="h"))
@@ -175,6 +131,19 @@ def test_view_tz(self):
175131
)
176132
tm.assert_series_equal(result, expected)
177133

134+
@pytest.mark.parametrize("tz", [None, "US/Central"])
135+
def test_asarray_object_dt64(self, tz):
136+
ser = pd.Series(pd.date_range("2000", periods=2, tz=tz))
137+
138+
with tm.assert_produces_warning(None):
139+
# Future behavior (for tzaware case) with no warning
140+
result = np.asarray(ser, dtype=object)
141+
142+
expected = np.array(
143+
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
144+
)
145+
tm.assert_numpy_array_equal(result, expected)
146+
178147
def test_asarray_tz_naive(self):
179148
# This shouldn't produce a warning.
180149
ser = pd.Series(pd.date_range("2000", periods=2))
@@ -183,12 +152,6 @@ def test_asarray_tz_naive(self):
183152

184153
tm.assert_numpy_array_equal(result, expected)
185154

186-
# optionally, object
187-
result = np.asarray(ser, dtype=object)
188-
189-
expected = np.array([pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02")])
190-
tm.assert_numpy_array_equal(result, expected)
191-
192155
def test_asarray_tz_aware(self):
193156
tz = "US/Central"
194157
ser = pd.Series(pd.date_range("2000", periods=2, tz=tz))
@@ -201,11 +164,3 @@ def test_asarray_tz_aware(self):
201164
result = np.asarray(ser, dtype="M8[ns]")
202165

203166
tm.assert_numpy_array_equal(result, expected)
204-
205-
# Future behavior with no warning
206-
expected = np.array(
207-
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
208-
)
209-
result = np.asarray(ser, dtype=object)
210-
211-
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)