Skip to content

Commit

Permalink
TST/REF: collect tests by method from test_io (#37451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Oct 29, 2020
1 parent d391e0b commit be1728a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 62 deletions.
27 changes: 25 additions & 2 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
from warnings import catch_warnings, simplefilter
import zipfile

import numpy as np
import pytest

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

import pandas as pd
from pandas import Index
from pandas import Index, Series, period_range
import pandas._testing as tm

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

def test_read_pickle_with_subclass():
# GH 12163
expected = pd.Series(dtype=object), MyTz()
expected = Series(dtype=object), MyTz()
result = tm.round_trip_pickle(expected)

tm.assert_series_equal(result[0], expected[0])
Expand Down Expand Up @@ -548,3 +549,25 @@ def _test_roundtrip(frame):
_test_roundtrip(frame.T)
_test_roundtrip(ymd)
_test_roundtrip(ymd.T)


def test_pickle_timeseries_periodindex():
# GH#2891
prng = period_range("1/1/2011", "1/1/2012", freq="M")
ts = Series(np.random.randn(len(prng)), prng)
new_ts = tm.round_trip_pickle(ts)
assert new_ts.index.freq == "M"


@pytest.mark.parametrize(
"name", [777, 777.0, "name", datetime.datetime(2001, 11, 11), (1, 2)]
)
def test_pickle_preserve_name(name):
def _pickle_roundtrip_name(obj):
with tm.ensure_clean() as path:
obj.to_pickle(path)
unpickled = pd.read_pickle(path)
return unpickled

unpickled = _pickle_roundtrip_name(tm.makeTimeSeries(name=name))
assert unpickled.name == name
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

import pandas as pd
from pandas import DataFrame, Series
from pandas import Series
import pandas._testing as tm

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

tm.assert_series_equal(result, expected)


class TestSeriesIO:
def test_to_frame(self, datetime_series):
datetime_series.name = None
rs = datetime_series.to_frame()
xp = pd.DataFrame(datetime_series.values, index=datetime_series.index)
tm.assert_frame_equal(rs, xp)

datetime_series.name = "testname"
rs = datetime_series.to_frame()
xp = pd.DataFrame(
dict(testname=datetime_series.values), index=datetime_series.index
)
tm.assert_frame_equal(rs, xp)

rs = datetime_series.to_frame(name="testdifferent")
xp = pd.DataFrame(
dict(testdifferent=datetime_series.values), index=datetime_series.index
)
tm.assert_frame_equal(rs, xp)

def test_timeseries_periodindex(self):
# GH2891
from pandas import period_range

prng = period_range("1/1/2011", "1/1/2012", freq="M")
ts = Series(np.random.randn(len(prng)), prng)
new_ts = tm.round_trip_pickle(ts)
assert new_ts.index.freq == "M"

def test_pickle_preserve_name(self):
for n in [777, 777.0, "name", datetime(2001, 11, 11), (1, 2)]:
unpickled = self._pickle_roundtrip_name(tm.makeTimeSeries(name=n))
assert unpickled.name == n

def _pickle_roundtrip_name(self, obj):

with tm.ensure_clean() as path:
obj.to_pickle(path)
unpickled = pd.read_pickle(path)
return unpickled

def test_to_frame_expanddim(self):
# GH 9762

class SubclassedSeries(Series):
@property
def _constructor_expanddim(self):
return SubclassedFrame

class SubclassedFrame(DataFrame):
pass

s = SubclassedSeries([1, 2, 3], name="X")
result = s.to_frame()
assert isinstance(result, SubclassedFrame)
expected = SubclassedFrame({"X": [1, 2, 3]})
tm.assert_frame_equal(result, expected)
40 changes: 40 additions & 0 deletions pandas/tests/series/methods/test_to_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pandas import DataFrame, Series
import pandas._testing as tm


class TestToFrame:
def test_to_frame(self, datetime_series):
datetime_series.name = None
rs = datetime_series.to_frame()
xp = DataFrame(datetime_series.values, index=datetime_series.index)
tm.assert_frame_equal(rs, xp)

datetime_series.name = "testname"
rs = datetime_series.to_frame()
xp = DataFrame(
dict(testname=datetime_series.values), index=datetime_series.index
)
tm.assert_frame_equal(rs, xp)

rs = datetime_series.to_frame(name="testdifferent")
xp = DataFrame(
dict(testdifferent=datetime_series.values), index=datetime_series.index
)
tm.assert_frame_equal(rs, xp)

def test_to_frame_expanddim(self):
# GH#9762

class SubclassedSeries(Series):
@property
def _constructor_expanddim(self):
return SubclassedFrame

class SubclassedFrame(DataFrame):
pass

ser = SubclassedSeries([1, 2, 3], name="X")
result = ser.to_frame()
assert isinstance(result, SubclassedFrame)
expected = SubclassedFrame({"X": [1, 2, 3]})
tm.assert_frame_equal(result, expected)

0 comments on commit be1728a

Please sign in to comment.