Skip to content

Commit 7453b63

Browse files
committed
TST: Use pytest idioms in series/test_dtypes.py
1 parent 424e609 commit 7453b63

File tree

2 files changed

+151
-122
lines changed

2 files changed

+151
-122
lines changed

pandas/tests/series/test_constructors.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -840,19 +840,24 @@ def test_constructor_cast_object(self):
840840
exp = Series(date_range('1/1/2000', periods=10))
841841
tm.assert_series_equal(s, exp)
842842

843-
def test_constructor_generic_timestamp(self):
843+
def test_constructor_generic_timestamp_deprecated(self):
844844
# see gh-15524
845-
dtype = np.timedelta64
846-
s = Series([], dtype=dtype)
847845

848-
assert s.empty
849-
assert s.dtype == 'm8[ns]'
846+
with tm.assert_produces_warning(FutureWarning,
847+
check_stacklevel=False):
848+
dtype = np.timedelta64
849+
s = Series([], dtype=dtype)
850850

851-
dtype = np.datetime64
852-
s = Series([], dtype=dtype)
851+
assert s.empty
852+
assert s.dtype == 'm8[ns]'
853853

854-
assert s.empty
855-
assert s.dtype == 'M8[ns]'
854+
with tm.assert_produces_warning(FutureWarning,
855+
check_stacklevel=False):
856+
dtype = np.datetime64
857+
s = Series([], dtype=dtype)
858+
859+
assert s.empty
860+
assert s.dtype == 'M8[ns]'
856861

857862
# These timestamps have the wrong frequencies,
858863
# so an Exception should be raised now.

pandas/tests/series/test_dtypes.py

+137-113
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# coding=utf-8
22
# pylint: disable-msg=E1101,W0612
33

4+
import pytest
5+
46
import sys
57
from datetime import datetime
68
import string
@@ -12,184 +14,207 @@
1214

1315
from pandas.compat import lrange, range, u
1416
from pandas import compat
15-
from pandas.util.testing import assert_series_equal
1617
import pandas.util.testing as tm
1718

1819
from .common import TestData
1920

2021

21-
class TestSeriesDtypes(TestData, tm.TestCase):
22+
class TestSeriesDtypes(TestData):
2223

23-
def test_astype(self):
24+
@pytest.mark.parametrize("dtype", ["float32", "float64",
25+
"int64", "int32"])
26+
def test_astype(self, dtype):
2427
s = Series(np.random.randn(5), name='foo')
28+
as_typed = s.astype(dtype)
2529

26-
for dtype in ['float32', 'float64', 'int64', 'int32']:
27-
astyped = s.astype(dtype)
28-
self.assertEqual(astyped.dtype, dtype)
29-
self.assertEqual(astyped.name, s.name)
30+
assert as_typed.dtype == dtype
31+
assert as_typed.name == s.name
3032

3133
def test_dtype(self):
3234

33-
self.assertEqual(self.ts.dtype, np.dtype('float64'))
34-
self.assertEqual(self.ts.dtypes, np.dtype('float64'))
35-
self.assertEqual(self.ts.ftype, 'float64:dense')
36-
self.assertEqual(self.ts.ftypes, 'float64:dense')
37-
assert_series_equal(self.ts.get_dtype_counts(), Series(1, ['float64']))
38-
assert_series_equal(self.ts.get_ftype_counts(), Series(
39-
1, ['float64:dense']))
40-
41-
def test_astype_cast_nan_inf_int(self):
42-
# GH14265, check nan and inf raise error when converting to int
43-
types = [np.int32, np.int64]
44-
values = [np.nan, np.inf]
35+
assert self.ts.dtype == np.dtype('float64')
36+
assert self.ts.dtypes == np.dtype('float64')
37+
assert self.ts.ftype == 'float64:dense'
38+
assert self.ts.ftypes == 'float64:dense'
39+
tm.assert_series_equal(self.ts.get_dtype_counts(),
40+
Series(1, ['float64']))
41+
tm.assert_series_equal(self.ts.get_ftype_counts(),
42+
Series(1, ['float64:dense']))
43+
44+
@pytest.mark.parametrize("value", [np.nan, np.inf])
45+
@pytest.mark.parametrize("dtype", [np.int32, np.int64])
46+
def test_astype_cast_nan_inf_int(self, dtype, value):
47+
# gh-14265: check NaN and inf raise error when converting to int
4548
msg = 'Cannot convert non-finite values \\(NA or inf\\) to integer'
49+
s = Series([value])
4650

47-
for this_type in types:
48-
for this_val in values:
49-
s = Series([this_val])
50-
with self.assertRaisesRegexp(ValueError, msg):
51-
s.astype(this_type)
51+
with tm.assertRaisesRegexp(ValueError, msg):
52+
s.astype(dtype)
5253

53-
def test_astype_cast_object_int(self):
54+
@pytest.mark.parametrize("dtype", [int, np.int8, np.int64])
55+
def test_astype_cast_object_int_fail(self, dtype):
5456
arr = Series(["car", "house", "tree", "1"])
57+
with pytest.raises(ValueError):
58+
arr.astype(dtype)
5559

56-
self.assertRaises(ValueError, arr.astype, int)
57-
self.assertRaises(ValueError, arr.astype, np.int64)
58-
self.assertRaises(ValueError, arr.astype, np.int8)
59-
60+
def test_astype_cast_object_int(self):
6061
arr = Series(['1', '2', '3', '4'], dtype=object)
6162
result = arr.astype(int)
62-
self.assert_series_equal(result, Series(np.arange(1, 5)))
63+
64+
tm.assert_series_equal(result, Series(np.arange(1, 5)))
6365

6466
def test_astype_datetimes(self):
6567
import pandas._libs.tslib as tslib
66-
6768
s = Series(tslib.iNaT, dtype='M8[ns]', index=lrange(5))
69+
6870
s = s.astype('O')
69-
self.assertEqual(s.dtype, np.object_)
71+
assert s.dtype == np.object_
7072

7173
s = Series([datetime(2001, 1, 2, 0, 0)])
74+
7275
s = s.astype('O')
73-
self.assertEqual(s.dtype, np.object_)
76+
assert s.dtype == np.object_
7477

7578
s = Series([datetime(2001, 1, 2, 0, 0) for i in range(3)])
79+
7680
s[1] = np.nan
77-
self.assertEqual(s.dtype, 'M8[ns]')
78-
s = s.astype('O')
79-
self.assertEqual(s.dtype, np.object_)
81+
assert s.dtype == 'M8[ns]'
8082

81-
def test_astype_str(self):
82-
# GH4405
83-
digits = string.digits
84-
s1 = Series([digits * 10, tm.rands(63), tm.rands(64), tm.rands(1000)])
85-
s2 = Series([digits * 10, tm.rands(63), tm.rands(64), nan, 1.0])
86-
types = (compat.text_type, np.str_)
87-
for typ in types:
88-
for s in (s1, s2):
89-
res = s.astype(typ)
90-
expec = s.map(compat.text_type)
91-
assert_series_equal(res, expec)
92-
93-
# GH9757
94-
# Test str and unicode on python 2.x and just str on python 3.x
95-
for tt in set([str, compat.text_type]):
96-
ts = Series([Timestamp('2010-01-04 00:00:00')])
97-
s = ts.astype(tt)
98-
expected = Series([tt('2010-01-04')])
99-
assert_series_equal(s, expected)
100-
101-
ts = Series([Timestamp('2010-01-04 00:00:00', tz='US/Eastern')])
102-
s = ts.astype(tt)
103-
expected = Series([tt('2010-01-04 00:00:00-05:00')])
104-
assert_series_equal(s, expected)
105-
106-
td = Series([Timedelta(1, unit='d')])
107-
s = td.astype(tt)
108-
expected = Series([tt('1 days 00:00:00.000000000')])
109-
assert_series_equal(s, expected)
83+
s = s.astype('O')
84+
assert s.dtype == np.object_
85+
86+
@pytest.mark.parametrize("dtype", [compat.text_type, np.str_])
87+
@pytest.mark.parametrize("series", [Series([string.digits * 10,
88+
tm.rands(63),
89+
tm.rands(64),
90+
tm.rands(1000)]),
91+
Series([string.digits * 10,
92+
tm.rands(63),
93+
tm.rands(64), nan, 1.0])])
94+
def test_astype_str_map(self, dtype, series):
95+
# see gh-4405
96+
result = series.astype(dtype)
97+
expected = series.map(compat.text_type)
98+
tm.assert_series_equal(result, expected)
99+
100+
@pytest.mark.parametrize("dtype", [str, compat.text_type])
101+
def test_astype_str_cast(self, dtype):
102+
# see gh-9757: test str and unicode on python 2.x
103+
# and just str on python 3.x
104+
ts = Series([Timestamp('2010-01-04 00:00:00')])
105+
s = ts.astype(dtype)
106+
107+
expected = Series([dtype('2010-01-04')])
108+
tm.assert_series_equal(s, expected)
109+
110+
ts = Series([Timestamp('2010-01-04 00:00:00', tz='US/Eastern')])
111+
s = ts.astype(dtype)
112+
113+
expected = Series([dtype('2010-01-04 00:00:00-05:00')])
114+
tm.assert_series_equal(s, expected)
115+
116+
td = Series([Timedelta(1, unit='d')])
117+
s = td.astype(dtype)
118+
119+
expected = Series([dtype('1 days 00:00:00.000000000')])
120+
tm.assert_series_equal(s, expected)
110121

111122
def test_astype_unicode(self):
112-
113-
# GH7758
114-
# a bit of magic is required to set default encoding encoding to utf-8
123+
# see gh-7758: A bit of magic is required to set
124+
# default encoding to utf-8
115125
digits = string.digits
116126
test_series = [
117127
Series([digits * 10, tm.rands(63), tm.rands(64), tm.rands(1000)]),
118128
Series([u('データーサイエンス、お前はもう死んでいる')]),
119-
120129
]
121130

122131
former_encoding = None
132+
123133
if not compat.PY3:
124-
# in python we can force the default encoding for this test
134+
# In Python, we can force the default encoding for this test
125135
former_encoding = sys.getdefaultencoding()
126136
reload(sys) # noqa
137+
127138
sys.setdefaultencoding("utf-8")
128139
if sys.getdefaultencoding() == "utf-8":
129140
test_series.append(Series([u('野菜食べないとやばい')
130141
.encode("utf-8")]))
142+
131143
for s in test_series:
132144
res = s.astype("unicode")
133145
expec = s.map(compat.text_type)
134-
assert_series_equal(res, expec)
135-
# restore the former encoding
146+
tm.assert_series_equal(res, expec)
147+
148+
# Restore the former encoding
136149
if former_encoding is not None and former_encoding != "utf-8":
137150
reload(sys) # noqa
138151
sys.setdefaultencoding(former_encoding)
139152

140153
def test_astype_dict(self):
141-
# GH7271
154+
# see gh-7271
142155
s = Series(range(0, 10, 2), name='abc')
143156

144157
result = s.astype({'abc': str})
145158
expected = Series(['0', '2', '4', '6', '8'], name='abc')
146-
assert_series_equal(result, expected)
159+
tm.assert_series_equal(result, expected)
147160

148161
result = s.astype({'abc': 'float64'})
149162
expected = Series([0.0, 2.0, 4.0, 6.0, 8.0], dtype='float64',
150163
name='abc')
151-
assert_series_equal(result, expected)
164+
tm.assert_series_equal(result, expected)
152165

153-
self.assertRaises(KeyError, s.astype, {'abc': str, 'def': str})
154-
self.assertRaises(KeyError, s.astype, {0: str})
166+
with pytest.raises(KeyError):
167+
s.astype({'abc': str, 'def': str})
155168

156-
def test_astype_generic_timestamp(self):
169+
with pytest.raises(KeyError):
170+
s.astype({0: str})
171+
172+
def test_astype_generic_timestamp_deprecated(self):
157173
# see gh-15524
158174
data = [1]
159175

160-
s = Series(data)
161-
dtype = np.datetime64
162-
result = s.astype(dtype)
163-
expected = Series(data, dtype=dtype)
164-
assert_series_equal(result, expected)
176+
with tm.assert_produces_warning(FutureWarning,
177+
check_stacklevel=False):
178+
s = Series(data)
179+
dtype = np.datetime64
180+
result = s.astype(dtype)
181+
expected = Series(data, dtype=dtype)
182+
tm.assert_series_equal(result, expected)
183+
184+
with tm.assert_produces_warning(FutureWarning,
185+
check_stacklevel=False):
186+
s = Series(data)
187+
dtype = np.timedelta64
188+
result = s.astype(dtype)
189+
expected = Series(data, dtype=dtype)
190+
tm.assert_series_equal(result, expected)
191+
192+
@pytest.mark.parametrize("dtype", np.typecodes['All'])
193+
def test_astype_empty_constructor_equality(self, dtype):
194+
# see gh-15524
165195

166-
s = Series(data)
167-
dtype = np.timedelta64
168-
result = s.astype(dtype)
169-
expected = Series(data, dtype=dtype)
170-
assert_series_equal(result, expected)
196+
if dtype not in ('S', 'V'): # poor support (if any) currently
197+
warning_type = None
171198

172-
def test_astype_empty_constructor_equality(self):
173-
# see gh-15524
199+
if dtype in ('M', 'm'):
200+
# Generic timestamp dtypes are deprecated.
201+
warning_type = FutureWarning
174202

175-
for dtype in np.typecodes['All']:
176-
if dtype not in ('S', 'V'): # poor support (if any) currently
203+
with tm.assert_produces_warning(warning_type,
204+
check_stacklevel=False):
177205
init_empty = Series([], dtype=dtype)
178-
astype_empty = Series([]).astype(dtype)
179206

180-
try:
181-
assert_series_equal(init_empty, astype_empty)
182-
except AssertionError as e:
183-
name = np.dtype(dtype).name
184-
msg = "{dtype} failed: ".format(dtype=name) + str(e)
207+
with tm.assert_produces_warning(warning_type,
208+
check_stacklevel=False):
209+
as_type_empty = Series([]).astype(dtype)
185210

186-
raise AssertionError(msg)
211+
tm.assert_series_equal(init_empty, as_type_empty)
187212

188-
def test_complexx(self):
189-
# GH4819
190-
# complex access for ndarray compat
213+
def test_complex(self):
214+
# see gh-4819: complex access for ndarray compat
191215
a = np.arange(5, dtype=np.float64)
192216
b = Series(a + 4j * a)
217+
193218
tm.assert_numpy_array_equal(a, b.real)
194219
tm.assert_numpy_array_equal(4 * a, b.imag)
195220

@@ -198,23 +223,22 @@ def test_complexx(self):
198223
tm.assert_numpy_array_equal(4 * a, b.imag)
199224

200225
def test_arg_for_errors_in_astype(self):
201-
# issue #14878
202-
203-
sr = Series([1, 2, 3])
226+
# see gh-14878
227+
s = Series([1, 2, 3])
204228

205-
with self.assertRaises(ValueError):
206-
sr.astype(np.float64, errors=False)
229+
with pytest.raises(ValueError):
230+
s.astype(np.float64, errors=False)
207231

208232
with tm.assert_produces_warning(FutureWarning):
209-
sr.astype(np.int8, raise_on_error=True)
233+
s.astype(np.int8, raise_on_error=True)
210234

211-
sr.astype(np.int8, errors='raise')
235+
s.astype(np.int8, errors='raise')
212236

213237
def test_intercept_astype_object(self):
214238
series = Series(date_range('1/1/2000', periods=10))
215239

216-
# this test no longer makes sense as series is by default already
217-
# M8[ns]
240+
# This test no longer makes sense, as
241+
# Series is by default already M8[ns].
218242
expected = series.astype('object')
219243

220244
df = DataFrame({'a': series,
@@ -224,9 +248,9 @@ def test_intercept_astype_object(self):
224248
tm.assert_series_equal(df.dtypes, exp_dtypes)
225249

226250
result = df.values.squeeze()
227-
self.assertTrue((result[:, 0] == expected.values).all())
251+
assert (result[:, 0] == expected.values).all()
228252

229253
df = DataFrame({'a': series, 'b': ['foo'] * len(series)})
230254

231255
result = df.values.squeeze()
232-
self.assertTrue((result[:, 0] == expected.values).all())
256+
assert (result[:, 0] == expected.values).all()

0 commit comments

Comments
 (0)