Skip to content

Commit be1728a

Browse files
authored
TST/REF: collect tests by method from test_io (#37451)
1 parent d391e0b commit be1728a

File tree

3 files changed

+66
-62
lines changed

3 files changed

+66
-62
lines changed

pandas/tests/io/test_pickle.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
from warnings import catch_warnings, simplefilter
2424
import zipfile
2525

26+
import numpy as np
2627
import pytest
2728

2829
from pandas.compat import PY38, get_lzma_file, import_lzma, is_platform_little_endian
2930
import pandas.util._test_decorators as td
3031

3132
import pandas as pd
32-
from pandas import Index
33+
from pandas import Index, Series, period_range
3334
import pandas._testing as tm
3435

3536
from pandas.tseries.offsets import Day, MonthEnd
@@ -499,7 +500,7 @@ def __init__(self):
499500

500501
def test_read_pickle_with_subclass():
501502
# GH 12163
502-
expected = pd.Series(dtype=object), MyTz()
503+
expected = Series(dtype=object), MyTz()
503504
result = tm.round_trip_pickle(expected)
504505

505506
tm.assert_series_equal(result[0], expected[0])
@@ -548,3 +549,25 @@ def _test_roundtrip(frame):
548549
_test_roundtrip(frame.T)
549550
_test_roundtrip(ymd)
550551
_test_roundtrip(ymd.T)
552+
553+
554+
def test_pickle_timeseries_periodindex():
555+
# GH#2891
556+
prng = period_range("1/1/2011", "1/1/2012", freq="M")
557+
ts = Series(np.random.randn(len(prng)), prng)
558+
new_ts = tm.round_trip_pickle(ts)
559+
assert new_ts.index.freq == "M"
560+
561+
562+
@pytest.mark.parametrize(
563+
"name", [777, 777.0, "name", datetime.datetime(2001, 11, 11), (1, 2)]
564+
)
565+
def test_pickle_preserve_name(name):
566+
def _pickle_roundtrip_name(obj):
567+
with tm.ensure_clean() as path:
568+
obj.to_pickle(path)
569+
unpickled = pd.read_pickle(path)
570+
return unpickled
571+
572+
unpickled = _pickle_roundtrip_name(tm.makeTimeSeries(name=name))
573+
assert unpickled.name == name

pandas/tests/series/test_io.py renamed to pandas/tests/series/methods/test_to_csv.py

+1-60
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
import pandas as pd
8-
from pandas import DataFrame, Series
8+
from pandas import Series
99
import pandas._testing as tm
1010

1111
from pandas.io.common import get_handle
@@ -180,62 +180,3 @@ def test_to_csv_interval_index(self):
180180
expected.index = expected.index.astype(str)
181181

182182
tm.assert_series_equal(result, expected)
183-
184-
185-
class TestSeriesIO:
186-
def test_to_frame(self, datetime_series):
187-
datetime_series.name = None
188-
rs = datetime_series.to_frame()
189-
xp = pd.DataFrame(datetime_series.values, index=datetime_series.index)
190-
tm.assert_frame_equal(rs, xp)
191-
192-
datetime_series.name = "testname"
193-
rs = datetime_series.to_frame()
194-
xp = pd.DataFrame(
195-
dict(testname=datetime_series.values), index=datetime_series.index
196-
)
197-
tm.assert_frame_equal(rs, xp)
198-
199-
rs = datetime_series.to_frame(name="testdifferent")
200-
xp = pd.DataFrame(
201-
dict(testdifferent=datetime_series.values), index=datetime_series.index
202-
)
203-
tm.assert_frame_equal(rs, xp)
204-
205-
def test_timeseries_periodindex(self):
206-
# GH2891
207-
from pandas import period_range
208-
209-
prng = period_range("1/1/2011", "1/1/2012", freq="M")
210-
ts = Series(np.random.randn(len(prng)), prng)
211-
new_ts = tm.round_trip_pickle(ts)
212-
assert new_ts.index.freq == "M"
213-
214-
def test_pickle_preserve_name(self):
215-
for n in [777, 777.0, "name", datetime(2001, 11, 11), (1, 2)]:
216-
unpickled = self._pickle_roundtrip_name(tm.makeTimeSeries(name=n))
217-
assert unpickled.name == n
218-
219-
def _pickle_roundtrip_name(self, obj):
220-
221-
with tm.ensure_clean() as path:
222-
obj.to_pickle(path)
223-
unpickled = pd.read_pickle(path)
224-
return unpickled
225-
226-
def test_to_frame_expanddim(self):
227-
# GH 9762
228-
229-
class SubclassedSeries(Series):
230-
@property
231-
def _constructor_expanddim(self):
232-
return SubclassedFrame
233-
234-
class SubclassedFrame(DataFrame):
235-
pass
236-
237-
s = SubclassedSeries([1, 2, 3], name="X")
238-
result = s.to_frame()
239-
assert isinstance(result, SubclassedFrame)
240-
expected = SubclassedFrame({"X": [1, 2, 3]})
241-
tm.assert_frame_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pandas import DataFrame, Series
2+
import pandas._testing as tm
3+
4+
5+
class TestToFrame:
6+
def test_to_frame(self, datetime_series):
7+
datetime_series.name = None
8+
rs = datetime_series.to_frame()
9+
xp = DataFrame(datetime_series.values, index=datetime_series.index)
10+
tm.assert_frame_equal(rs, xp)
11+
12+
datetime_series.name = "testname"
13+
rs = datetime_series.to_frame()
14+
xp = DataFrame(
15+
dict(testname=datetime_series.values), index=datetime_series.index
16+
)
17+
tm.assert_frame_equal(rs, xp)
18+
19+
rs = datetime_series.to_frame(name="testdifferent")
20+
xp = DataFrame(
21+
dict(testdifferent=datetime_series.values), index=datetime_series.index
22+
)
23+
tm.assert_frame_equal(rs, xp)
24+
25+
def test_to_frame_expanddim(self):
26+
# GH#9762
27+
28+
class SubclassedSeries(Series):
29+
@property
30+
def _constructor_expanddim(self):
31+
return SubclassedFrame
32+
33+
class SubclassedFrame(DataFrame):
34+
pass
35+
36+
ser = SubclassedSeries([1, 2, 3], name="X")
37+
result = ser.to_frame()
38+
assert isinstance(result, SubclassedFrame)
39+
expected = SubclassedFrame({"X": [1, 2, 3]})
40+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)