Skip to content

Commit 86bd897

Browse files
committed
Parametrized all series tests in TestSetitemCoercion
1 parent a3a8327 commit 86bd897

File tree

1 file changed

+94
-181
lines changed

1 file changed

+94
-181
lines changed

pandas/tests/indexing/test_coercion.py

+94-181
Original file line numberDiff line numberDiff line change
@@ -62,241 +62,154 @@ def _assert_setitem_series_conversion(self, original_series, loc_value,
6262
# temp.loc[1] = loc_value
6363
# tm.assert_series_equal(temp, expected_series)
6464

65-
def test_setitem_series_object(self):
65+
@pytest.mark.parametrize("val,exp_dtype", [
66+
(1, np.object),
67+
(1.1, np.object),
68+
(1 + 1j, np.object),
69+
(True, np.object)])
70+
def test_setitem_series_object(self, val, exp_dtype):
6671
obj = pd.Series(list('abcd'))
6772
assert obj.dtype == np.object
6873

69-
# object + int -> object
70-
exp = pd.Series(['a', 1, 'c', 'd'])
71-
self._assert_setitem_series_conversion(obj, 1, exp, np.object)
72-
73-
# object + float -> object
74-
exp = pd.Series(['a', 1.1, 'c', 'd'])
75-
self._assert_setitem_series_conversion(obj, 1.1, exp, np.object)
76-
77-
# object + complex -> object
78-
exp = pd.Series(['a', 1 + 1j, 'c', 'd'])
79-
self._assert_setitem_series_conversion(obj, 1 + 1j, exp, np.object)
80-
81-
# object + bool -> object
82-
exp = pd.Series(['a', True, 'c', 'd'])
83-
self._assert_setitem_series_conversion(obj, True, exp, np.object)
84-
85-
def test_setitem_series_int64(self):
74+
exp = pd.Series(['a', val, 'c', 'd'])
75+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
76+
77+
@pytest.mark.parametrize("val,exp_dtype", [
78+
(1, np.int64),
79+
(1.1, np.float64),
80+
(1 + 1j, np.complex128),
81+
(True, np.object)])
82+
def test_setitem_series_int64(self, val, exp_dtype):
8683
obj = pd.Series([1, 2, 3, 4])
8784
assert obj.dtype == np.int64
8885

89-
# int + int -> int
90-
exp = pd.Series([1, 1, 3, 4])
91-
self._assert_setitem_series_conversion(obj, 1, exp, np.int64)
92-
93-
# int + float -> float
94-
# TODO_GH12747 The result must be float
95-
# tm.assert_series_equal(temp, pd.Series([1, 1.1, 3, 4]))
96-
# assert temp.dtype == np.float64
97-
exp = pd.Series([1, 1, 3, 4])
98-
self._assert_setitem_series_conversion(obj, 1.1, exp, np.int64)
99-
100-
# int + complex -> complex
101-
exp = pd.Series([1, 1 + 1j, 3, 4])
102-
self._assert_setitem_series_conversion(obj, 1 + 1j, exp, np.complex128)
86+
if exp_dtype is np.float64:
87+
exp = pd.Series([1, 1, 3, 4])
88+
self._assert_setitem_series_conversion(obj, 1.1, exp, np.int64)
89+
pytest.xfail("GH12747 The result must be float")
10390

104-
# int + bool -> object
105-
exp = pd.Series([1, True, 3, 4])
106-
self._assert_setitem_series_conversion(obj, True, exp, np.object)
91+
exp = pd.Series([1, val, 3, 4])
92+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
10793

108-
def test_setitem_series_int8(self):
94+
@pytest.mark.parametrize("val,exp_dtype", [
95+
(np.int32(1), np.int8),
96+
(np.int16(2**9), np.int16)])
97+
def test_setitem_series_int8(self, val, exp_dtype):
10998
# integer dtype coercion (no change)
11099
obj = pd.Series([1, 2, 3, 4], dtype=np.int8)
111100
assert obj.dtype == np.int8
112101

113-
exp = pd.Series([1, 1, 3, 4], dtype=np.int8)
114-
self._assert_setitem_series_conversion(obj, np.int32(1), exp, np.int8)
115-
116-
# BUG: it must be Series([1, 1, 3, 4], dtype=np.int16)
117-
exp = pd.Series([1, 0, 3, 4], dtype=np.int8)
118-
self._assert_setitem_series_conversion(obj, np.int16(2**9), exp,
119-
np.int8)
102+
if exp_dtype is np.int16:
103+
exp = pd.Series([1, 0, 3, 4], dtype=np.int8)
104+
self._assert_setitem_series_conversion(obj, val, exp, np.int8)
105+
pytest.xfail("BUG: it must be Series([1, 1, 3, 4], dtype=np.int16")
106+
107+
exp = pd.Series([1, val, 3, 4], dtype=np.int8)
108+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
120109

121-
def test_setitem_series_float64(self):
110+
@pytest.mark.parametrize("val,exp_dtype", [
111+
(1, np.float64),
112+
(1.1, np.float64),
113+
(1 + 1j, np.complex128),
114+
(True, np.object)])
115+
def test_setitem_series_float64(self, val, exp_dtype):
122116
obj = pd.Series([1.1, 2.2, 3.3, 4.4])
123117
assert obj.dtype == np.float64
124118

125-
# float + int -> float
126-
exp = pd.Series([1.1, 1.0, 3.3, 4.4])
127-
self._assert_setitem_series_conversion(obj, 1, exp, np.float64)
128-
129-
# float + float -> float
130-
exp = pd.Series([1.1, 1.1, 3.3, 4.4])
131-
self._assert_setitem_series_conversion(obj, 1.1, exp, np.float64)
132-
133-
# float + complex -> complex
134-
exp = pd.Series([1.1, 1 + 1j, 3.3, 4.4])
135-
self._assert_setitem_series_conversion(obj, 1 + 1j, exp,
136-
np.complex128)
137-
138-
# float + bool -> object
139-
exp = pd.Series([1.1, True, 3.3, 4.4])
140-
self._assert_setitem_series_conversion(obj, True, exp, np.object)
119+
exp = pd.Series([1.1, val, 3.3, 4.4])
120+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
141121

142-
def test_setitem_series_complex128(self):
122+
@pytest.mark.parametrize("val,exp_dtype", [
123+
(1, np.complex128),
124+
(1.1, np.complex128),
125+
(1 + 1j, np.complex128),
126+
(True, np.object)])
127+
def test_setitem_series_complex128(self, val, exp_dtype):
143128
obj = pd.Series([1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j])
144129
assert obj.dtype == np.complex128
145130

146-
# complex + int -> complex
147-
exp = pd.Series([1 + 1j, 1, 3 + 3j, 4 + 4j])
148-
self._assert_setitem_series_conversion(obj, 1, exp, np.complex128)
149-
150-
# complex + float -> complex
151-
exp = pd.Series([1 + 1j, 1.1, 3 + 3j, 4 + 4j])
152-
self._assert_setitem_series_conversion(obj, 1.1, exp, np.complex128)
153-
154-
# complex + complex -> complex
155-
exp = pd.Series([1 + 1j, 1 + 1j, 3 + 3j, 4 + 4j])
156-
self._assert_setitem_series_conversion(obj, 1 + 1j, exp, np.complex128)
131+
exp = pd.Series([1 + 1j, val, 3 + 3j, 4 + 4j])
132+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
157133

158-
# complex + bool -> object
159-
exp = pd.Series([1 + 1j, True, 3 + 3j, 4 + 4j])
160-
self._assert_setitem_series_conversion(obj, True, exp, np.object)
161-
162-
def test_setitem_series_bool(self):
134+
@pytest.mark.parametrize("val,exp_dtype", [
135+
(1, np.int64),
136+
(3, np.int64),
137+
(1.1, np.float64),
138+
(1 + 1j, np.complex128),
139+
(True, np.bool)])
140+
def test_setitem_series_bool(self, val, exp_dtype):
163141
obj = pd.Series([True, False, True, False])
164142
assert obj.dtype == np.bool
165143

166-
# bool + int -> int
167-
# TODO_GH12747 The result must be int
168-
# tm.assert_series_equal(temp, pd.Series([1, 1, 1, 0]))
169-
# assert temp.dtype == np.int64
170-
exp = pd.Series([True, True, True, False])
171-
self._assert_setitem_series_conversion(obj, 1, exp, np.bool)
172-
173-
# TODO_GH12747 The result must be int
174-
# assigning int greater than bool
175-
# tm.assert_series_equal(temp, pd.Series([1, 3, 1, 0]))
176-
# assert temp.dtype == np.int64
177-
exp = pd.Series([True, True, True, False])
178-
self._assert_setitem_series_conversion(obj, 3, exp, np.bool)
179-
180-
# bool + float -> float
181-
# TODO_GH12747 The result must be float
182-
# tm.assert_series_equal(temp, pd.Series([1., 1.1, 1., 0.]))
183-
# assert temp.dtype == np.float64
184-
exp = pd.Series([True, True, True, False])
185-
self._assert_setitem_series_conversion(obj, 1.1, exp, np.bool)
186-
187-
# bool + complex -> complex (buggy, results in bool)
188-
# TODO_GH12747 The result must be complex
189-
# tm.assert_series_equal(temp, pd.Series([1, 1 + 1j, 1, 0]))
190-
# assert temp.dtype == np.complex128
191-
exp = pd.Series([True, True, True, False])
192-
self._assert_setitem_series_conversion(obj, 1 + 1j, exp, np.bool)
193-
194-
# bool + bool -> bool
195-
exp = pd.Series([True, True, True, False])
196-
self._assert_setitem_series_conversion(obj, True, exp, np.bool)
197-
198-
def test_setitem_series_datetime64(self):
144+
if exp_dtype is np.int64:
145+
exp = pd.Series([True, True, True, False])
146+
self._assert_setitem_series_conversion(obj, val, exp, np.bool)
147+
pytest.xfail("TODO_GH12747 The result must be int")
148+
elif exp_dtype is np.float64:
149+
exp = pd.Series([True, True, True, False])
150+
self._assert_setitem_series_conversion(obj, val, exp, np.bool)
151+
pytest.xfail("TODO_GH12747 The result must be float")
152+
elif exp_dtype is np.complex128:
153+
exp = pd.Series([True, True, True, False])
154+
self._assert_setitem_series_conversion(obj, val, exp, np.bool)
155+
pytest.xfail("TODO_GH12747 The result must be complex")
156+
157+
exp = pd.Series([True, val, True, False])
158+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
159+
160+
@pytest.mark.parametrize("val,exp_dtype", [
161+
(pd.Timestamp('2012-01-01'), 'datetime64[ns]'),
162+
(1, np.object),
163+
('x', np.object)])
164+
def test_setitem_series_datetime64(self, val, exp_dtype):
199165
obj = pd.Series([pd.Timestamp('2011-01-01'),
200166
pd.Timestamp('2011-01-02'),
201167
pd.Timestamp('2011-01-03'),
202168
pd.Timestamp('2011-01-04')])
203169
assert obj.dtype == 'datetime64[ns]'
204170

205-
# datetime64 + datetime64 -> datetime64
206-
exp = pd.Series([pd.Timestamp('2011-01-01'),
207-
pd.Timestamp('2012-01-01'),
208-
pd.Timestamp('2011-01-03'),
209-
pd.Timestamp('2011-01-04')])
210-
self._assert_setitem_series_conversion(obj, pd.Timestamp('2012-01-01'),
211-
exp, 'datetime64[ns]')
212-
213-
# datetime64 + int -> object
214-
exp = pd.Series([pd.Timestamp('2011-01-01'),
215-
1,
216-
pd.Timestamp('2011-01-03'),
217-
pd.Timestamp('2011-01-04')])
218-
self._assert_setitem_series_conversion(obj, 1, exp, 'object')
219-
220-
# datetime64 + object -> object
221171
exp = pd.Series([pd.Timestamp('2011-01-01'),
222-
'x',
172+
val,
223173
pd.Timestamp('2011-01-03'),
224174
pd.Timestamp('2011-01-04')])
225-
self._assert_setitem_series_conversion(obj, 'x', exp, np.object)
175+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
226176

227-
def test_setitem_series_datetime64tz(self):
177+
@pytest.mark.parametrize("val,exp_dtype", [
178+
(pd.Timestamp('2012-01-01', tz='US/Eastern'),
179+
'datetime64[ns, US/Eastern]'),
180+
(pd.Timestamp('2012-01-01', tz='US/Pacific'), np.object),
181+
(pd.Timestamp('2012-01-01'), np.object),
182+
(1, np.object)])
183+
def test_setitem_series_datetime64tz(self, val, exp_dtype):
228184
tz = 'US/Eastern'
229185
obj = pd.Series([pd.Timestamp('2011-01-01', tz=tz),
230186
pd.Timestamp('2011-01-02', tz=tz),
231187
pd.Timestamp('2011-01-03', tz=tz),
232188
pd.Timestamp('2011-01-04', tz=tz)])
233189
assert obj.dtype == 'datetime64[ns, US/Eastern]'
234190

235-
# datetime64tz + datetime64tz -> datetime64tz
236-
exp = pd.Series([pd.Timestamp('2011-01-01', tz=tz),
237-
pd.Timestamp('2012-01-01', tz=tz),
238-
pd.Timestamp('2011-01-03', tz=tz),
239-
pd.Timestamp('2011-01-04', tz=tz)])
240-
value = pd.Timestamp('2012-01-01', tz=tz)
241-
self._assert_setitem_series_conversion(obj, value, exp,
242-
'datetime64[ns, US/Eastern]')
243-
244-
# datetime64tz + datetime64tz (different tz) -> object
245-
exp = pd.Series([pd.Timestamp('2011-01-01', tz=tz),
246-
pd.Timestamp('2012-01-01', tz='US/Pacific'),
247-
pd.Timestamp('2011-01-03', tz=tz),
248-
pd.Timestamp('2011-01-04', tz=tz)])
249-
value = pd.Timestamp('2012-01-01', tz='US/Pacific')
250-
self._assert_setitem_series_conversion(obj, value, exp, np.object)
251-
252-
# datetime64tz + datetime64 -> object
253-
exp = pd.Series([pd.Timestamp('2011-01-01', tz=tz),
254-
pd.Timestamp('2012-01-01'),
255-
pd.Timestamp('2011-01-03', tz=tz),
256-
pd.Timestamp('2011-01-04', tz=tz)])
257-
value = pd.Timestamp('2012-01-01')
258-
self._assert_setitem_series_conversion(obj, value, exp, np.object)
259-
260-
# datetime64 + int -> object
261191
exp = pd.Series([pd.Timestamp('2011-01-01', tz=tz),
262-
1,
192+
val,
263193
pd.Timestamp('2011-01-03', tz=tz),
264194
pd.Timestamp('2011-01-04', tz=tz)])
265-
self._assert_setitem_series_conversion(obj, 1, exp, np.object)
266-
267-
# ToDo: add more tests once the above issue has been fixed
195+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
268196

269-
def test_setitem_series_timedelta64(self):
197+
@pytest.mark.parametrize("val,exp_dtype", [
198+
(pd.Timedelta('12 day'), 'timedelta64[ns]'),
199+
(1, np.object),
200+
('x', np.object)])
201+
def test_setitem_series_timedelta64(self, val, exp_dtype):
270202
obj = pd.Series([pd.Timedelta('1 day'),
271203
pd.Timedelta('2 day'),
272204
pd.Timedelta('3 day'),
273205
pd.Timedelta('4 day')])
274206
assert obj.dtype == 'timedelta64[ns]'
275207

276-
# timedelta64 + timedelta64 -> timedelta64
277208
exp = pd.Series([pd.Timedelta('1 day'),
278-
pd.Timedelta('12 day'),
209+
val,
279210
pd.Timedelta('3 day'),
280211
pd.Timedelta('4 day')])
281-
self._assert_setitem_series_conversion(obj, pd.Timedelta('12 day'),
282-
exp, 'timedelta64[ns]')
283-
284-
# timedelta64 + int -> object
285-
exp = pd.Series([pd.Timedelta('1 day'),
286-
1,
287-
pd.Timedelta('3 day'),
288-
pd.Timedelta('4 day')])
289-
self._assert_setitem_series_conversion(obj, 1, exp, np.object)
290-
291-
# timedelta64 + object -> object
292-
exp = pd.Series([pd.Timedelta('1 day'),
293-
'x',
294-
pd.Timedelta('3 day'),
295-
pd.Timedelta('4 day')])
296-
self._assert_setitem_series_conversion(obj, 'x', exp, np.object)
297-
298-
def test_setitem_series_period(self):
299-
pass
212+
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
300213

301214
def _assert_setitem_index_conversion(self, original_series, loc_key,
302215
expected_index, expected_dtype):

0 commit comments

Comments
 (0)