Skip to content

Commit 5f0acc5

Browse files
committed
MAINT: Move round_trip_pickle outside of TestCase
1 parent 615712c commit 5f0acc5

20 files changed

+42
-29
lines changed

pandas/tests/core/dtypes/test_dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def f():
4040
self.assertNotEqual(np.str_, self.dtype)
4141

4242
def test_pickle(self):
43-
result = self.round_trip_pickle(self.dtype)
43+
result = tm.round_trip_pickle(self.dtype)
4444
self.assertEqual(result, self.dtype)
4545

4646

pandas/tests/core/sparse/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def _check_inplace_op(op):
562562

563563
def test_pickle(self):
564564
def _check_roundtrip(obj):
565-
unpickled = self.round_trip_pickle(obj)
565+
unpickled = tm.round_trip_pickle(obj)
566566
tm.assert_sp_array_equal(unpickled, obj)
567567

568568
_check_roundtrip(self.arr)

pandas/tests/core/sparse/test_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def test_array_interface(self):
278278
def test_pickle(self):
279279

280280
def _test_roundtrip(frame, orig):
281-
result = self.round_trip_pickle(frame)
281+
result = tm.round_trip_pickle(frame)
282282
tm.assert_sp_frame_equal(frame, result)
283283
tm.assert_frame_equal(result.to_dense(), orig, check_dtype=False)
284284

pandas/tests/core/sparse/test_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def test_to_frame(self):
390390

391391
def test_pickle(self):
392392
def _test_roundtrip(series):
393-
unpickled = self.round_trip_pickle(series)
393+
unpickled = tm.round_trip_pickle(series)
394394
tm.assert_sp_series_equal(series, unpickled)
395395
tm.assert_series_equal(series.to_dense(), unpickled.to_dense())
396396

pandas/tests/frame/test_block_internals.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,18 @@ def test_copy(self):
350350
self.assertIsNot(copy._data, self.mixed_frame._data)
351351

352352
def test_pickle(self):
353-
unpickled = self.round_trip_pickle(self.mixed_frame)
353+
unpickled = tm.round_trip_pickle(self.mixed_frame)
354354
assert_frame_equal(self.mixed_frame, unpickled)
355355

356356
# buglet
357357
self.mixed_frame._data.ndim
358358

359359
# empty
360-
unpickled = self.round_trip_pickle(self.empty)
360+
unpickled = tm.round_trip_pickle(self.empty)
361361
repr(unpickled)
362362

363363
# tz frame
364-
unpickled = self.round_trip_pickle(self.tzframe)
364+
unpickled = tm.round_trip_pickle(self.tzframe)
365365
assert_frame_equal(self.tzframe, unpickled)
366366

367367
def test_consolidate_datetime64(self):

pandas/tests/frame/test_subclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_dataframe_metadata(self):
8585
self.assertEqual(df.iloc[0:1, :].testattr, 'XXX')
8686

8787
# GH10553
88-
unpickled = self.round_trip_pickle(df)
88+
unpickled = tm.round_trip_pickle(df)
8989
tm.assert_frame_equal(df, unpickled)
9090
self.assertEqual(df._metadata, unpickled._metadata)
9191
self.assertEqual(df.testattr, unpickled.testattr)

pandas/tests/indexes/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def setup_indices(self):
2828
setattr(self, name, idx)
2929

3030
def verify_pickle(self, index):
31-
unpickled = self.round_trip_pickle(index)
31+
unpickled = tm.round_trip_pickle(index)
3232
self.assertTrue(index.equals(unpickled))
3333

3434
def test_pickle_compat_construction(self):

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_roundtrip_pickle_with_tz(self):
106106
# GH 8367
107107
# round-trip of timezone
108108
index = date_range('20130101', periods=3, tz='US/Eastern', name='foo')
109-
unpickled = self.round_trip_pickle(index)
109+
unpickled = tm.round_trip_pickle(index)
110110
self.assert_index_equal(index, unpickled)
111111

112112
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):

pandas/tests/indexes/datetimes/test_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ def test_comparison(self):
11211121
self.assertFalse(comp[9])
11221122

11231123
def test_pickle_unpickle(self):
1124-
unpickled = self.round_trip_pickle(self.rng)
1124+
unpickled = tm.round_trip_pickle(self.rng)
11251125
self.assertIsNotNone(unpickled.offset)
11261126

11271127
def test_copy(self):
@@ -1272,7 +1272,7 @@ def test_shift(self):
12721272
self.assertEqual(shifted[0], rng[0] + CDay())
12731273

12741274
def test_pickle_unpickle(self):
1275-
unpickled = self.round_trip_pickle(self.rng)
1275+
unpickled = tm.round_trip_pickle(self.rng)
12761276
self.assertIsNotNone(unpickled.offset)
12771277

12781278
def test_summary(self):

pandas/tests/indexes/period/test_period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_pickle_compat_construction(self):
5858
def test_pickle_round_trip(self):
5959
for freq in ['D', 'M', 'Y']:
6060
idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D')
61-
result = self.round_trip_pickle(idx)
61+
result = tm.round_trip_pickle(idx)
6262
tm.assert_index_equal(result, idx)
6363

6464
def test_get_loc(self):
@@ -761,7 +761,7 @@ def test_append_concat(self):
761761
def test_pickle_freq(self):
762762
# GH2891
763763
prng = period_range('1/1/2011', '1/1/2012', freq='M')
764-
new_prng = self.round_trip_pickle(prng)
764+
new_prng = tm.round_trip_pickle(prng)
765765
self.assertEqual(new_prng.freq, offsets.MonthEnd())
766766
self.assertEqual(new_prng.freqstr, 'M')
767767

pandas/tests/indexes/test_multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def test_roundtrip_pickle_with_tz(self):
10441044
[[1, 2], ['a', 'b'], date_range('20130101', periods=3,
10451045
tz='US/Eastern')
10461046
], names=['one', 'two', 'three'])
1047-
unpickled = self.round_trip_pickle(index)
1047+
unpickled = tm.round_trip_pickle(index)
10481048
self.assertTrue(index.equal_levels(unpickled))
10491049

10501050
def test_from_tuples_index_values(self):

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def test_pass_TimedeltaIndex_to_index(self):
454454
def test_pickle(self):
455455

456456
rng = timedelta_range('1 days', periods=10)
457-
rng_p = self.round_trip_pickle(rng)
457+
rng_p = tm.round_trip_pickle(rng)
458458
tm.assert_index_equal(rng, rng_p)
459459

460460
def test_hash_error(self):

pandas/tests/scalar/test_period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def test_multiples(self):
909909
def test_round_trip(self):
910910

911911
p = Period('2000Q1')
912-
new_p = self.round_trip_pickle(p)
912+
new_p = tm.round_trip_pickle(p)
913913
self.assertEqual(new_p, p)
914914

915915

pandas/tests/scalar/test_timedelta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def test_overflow(self):
559559
def test_pickle(self):
560560

561561
v = Timedelta('1 days 10:11:12.0123456')
562-
v_p = self.round_trip_pickle(v)
562+
v_p = tm.round_trip_pickle(v)
563563
self.assertEqual(v, v_p)
564564

565565
def test_timedelta_hash_equality(self):

pandas/tests/series/test_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_timeseries_periodindex(self):
134134
from pandas import period_range
135135
prng = period_range('1/1/2011', '1/1/2012', freq='M')
136136
ts = Series(np.random.randn(len(prng)), prng)
137-
new_ts = self.round_trip_pickle(ts)
137+
new_ts = tm.round_trip_pickle(ts)
138138
self.assertEqual(new_ts.index.freq, 'M')
139139

140140
def test_pickle_preserve_name(self):

pandas/tests/series/test_timeseries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,19 +827,19 @@ def test_asfreq_resample_set_correct_freq(self):
827827
def test_pickle(self):
828828

829829
# GH4606
830-
p = self.round_trip_pickle(NaT)
830+
p = tm.round_trip_pickle(NaT)
831831
self.assertTrue(p is NaT)
832832

833833
idx = pd.to_datetime(['2013-01-01', NaT, '2014-01-06'])
834-
idx_p = self.round_trip_pickle(idx)
834+
idx_p = tm.round_trip_pickle(idx)
835835
self.assertTrue(idx_p[0] == idx[0])
836836
self.assertTrue(idx_p[1] is NaT)
837837
self.assertTrue(idx_p[2] == idx[2])
838838

839839
# GH11002
840840
# don't infer freq
841841
idx = date_range('1750-1-1', '2050-1-1', freq='7D')
842-
idx_p = self.round_trip_pickle(idx)
842+
idx_p = tm.round_trip_pickle(idx)
843843
tm.assert_index_equal(idx, idx_p)
844844

845845
def test_setops_preserve_freq(self):

pandas/tests/test_multilevel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _check_op(opname):
179179

180180
def test_pickle(self):
181181
def _test_roundtrip(frame):
182-
unpickled = self.round_trip_pickle(frame)
182+
unpickled = tm.round_trip_pickle(frame)
183183
tm.assert_frame_equal(frame, unpickled)
184184

185185
_test_roundtrip(self.frame)

pandas/tests/test_panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PanelTests(object):
4343

4444
def test_pickle(self):
4545
with catch_warnings(record=True):
46-
unpickled = self.round_trip_pickle(self.panel)
46+
unpickled = tm.round_trip_pickle(self.panel)
4747
assert_frame_equal(unpickled['ItemA'], self.panel['ItemA'])
4848

4949
def test_rank(self):

pandas/tests/tseries/test_offsets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,7 +1906,7 @@ def test_calendar(self):
19061906

19071907
def test_roundtrip_pickle(self):
19081908
def _check_roundtrip(obj):
1909-
unpickled = self.round_trip_pickle(obj)
1909+
unpickled = tm.round_trip_pickle(obj)
19101910
self.assertEqual(unpickled, obj)
19111911

19121912
_check_roundtrip(self.offset)
@@ -1967,7 +1967,7 @@ def test_offsets_compare_equal(self):
19671967

19681968
def test_roundtrip_pickle(self):
19691969
def _check_roundtrip(obj):
1970-
unpickled = self.round_trip_pickle(obj)
1970+
unpickled = tm.round_trip_pickle(obj)
19711971
self.assertEqual(unpickled, obj)
19721972

19731973
_check_roundtrip(self._object())

pandas/util/testing.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ def setUpClass(cls):
9292
def tearDownClass(cls):
9393
pass
9494

95-
def round_trip_pickle(self, obj, path=None):
96-
return round_trip_pickle(obj, path=path)
97-
9895
# https://docs.python.org/3/library/unittest.html#deprecated-aliases
9996
def assertEquals(self, *args, **kwargs):
10097
return deprecate('assertEquals',
@@ -126,6 +123,22 @@ def reset_display_options():
126123

127124

128125
def round_trip_pickle(obj, path=None):
126+
"""
127+
Pickle an object and then read it again.
128+
129+
Parameters
130+
----------
131+
obj : pandas object
132+
The object to pickle and then re-read.
133+
path : str, default None
134+
The path where the pickled object is written and then read.
135+
136+
Returns
137+
-------
138+
round_trip_pickled_object : pandas object
139+
The original object that was pickled and then re-read.
140+
"""
141+
129142
if path is None:
130143
path = u('__%s__.pickle' % rands(10))
131144
with ensure_clean(path) as path:

0 commit comments

Comments
 (0)