Skip to content

Commit 87dd4eb

Browse files
committed
Cleaned up comments; LINT
1 parent bca2240 commit 87dd4eb

File tree

1 file changed

+41
-44
lines changed

1 file changed

+41
-44
lines changed

pandas/tests/indexing/test_coercion.py

+41-44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Index / Series common tests which may trigger dtype coercions
1414
###############################################################
1515

16+
1617
@pytest.fixture(autouse=True, scope='class')
1718
def check_comprehensiveness(request):
1819
# Iterate over combination of dtype, method and klass
@@ -30,9 +31,10 @@ def has_test(combo):
3031
if not has_test(combo):
3132
msg = 'test method is not defined: {0}, {1}'
3233
raise AssertionError(msg.format(type(cls), combo))
33-
34+
3435
yield
3536

37+
3638
class CoercionBase(object):
3739

3840
klasses = ['index', 'series']
@@ -84,7 +86,7 @@ def test_setitem_series_object(self, val, exp_dtype):
8486

8587
exp = pd.Series(['a', val, 'c', 'd'])
8688
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
87-
89+
8890
@pytest.mark.parametrize("val,exp_dtype", [
8991
(1, np.int64),
9092
(1.1, np.float64),
@@ -106,15 +108,14 @@ def test_setitem_series_int64(self, val, exp_dtype):
106108
(np.int32(1), np.int8),
107109
(np.int16(2**9), np.int16)])
108110
def test_setitem_series_int8(self, val, exp_dtype):
109-
# integer dtype coercion (no change)
110111
obj = pd.Series([1, 2, 3, 4], dtype=np.int8)
111112
assert obj.dtype == np.int8
112113

113114
if exp_dtype is np.int16:
114115
exp = pd.Series([1, 0, 3, 4], dtype=np.int8)
115116
self._assert_setitem_series_conversion(obj, val, exp, np.int8)
116117
pytest.xfail("BUG: it must be Series([1, 1, 3, 4], dtype=np.int16")
117-
118+
118119
exp = pd.Series([1, val, 3, 4], dtype=np.int8)
119120
self._assert_setitem_series_conversion(obj, val, exp, exp_dtype)
120121

@@ -159,7 +160,7 @@ def test_setitem_series_bool(self, val, exp_dtype):
159160
elif exp_dtype is np.float64:
160161
exp = pd.Series([True, True, True, False])
161162
self._assert_setitem_series_conversion(obj, val, exp, np.bool)
162-
pytest.xfail("TODO_GH12747 The result must be float")
163+
pytest.xfail("TODO_GH12747 The result must be float")
163164
elif exp_dtype is np.complex128:
164165
exp = pd.Series([True, True, True, False])
165166
self._assert_setitem_series_conversion(obj, val, exp, np.bool)
@@ -248,20 +249,19 @@ def test_setitem_index_object(self, val, exp_dtype):
248249
assert obj.index.dtype == np.object
249250

250251
if exp_dtype is IndexError:
251-
# object + int -> IndexError, regarded as location
252252
temp = obj.copy()
253253
with pytest.raises(exp_dtype):
254254
temp[5] = 5
255255
else:
256256
exp_index = pd.Index(list('abcd') + [val])
257-
self._assert_setitem_index_conversion(obj, val, exp_index, exp_dtype)
257+
self._assert_setitem_index_conversion(obj, val, exp_index,
258+
exp_dtype)
258259

259260
@pytest.mark.parametrize("val,exp_dtype", [
260261
(5, np.int64),
261262
(1.1, np.float64),
262263
('x', np.object)])
263264
def test_setitem_index_int64(self, val, exp_dtype):
264-
# tests setitem with non-existing numeric key
265265
obj = pd.Series([1, 2, 3, 4])
266266
assert obj.index.dtype == np.int64
267267

@@ -273,7 +273,6 @@ def test_setitem_index_int64(self, val, exp_dtype):
273273
(5.1, np.float64),
274274
('x', np.object)])
275275
def test_setitem_index_float64(self, val, exp_dtype):
276-
# tests setitem with non-existing numeric key
277276
obj = pd.Series([1, 2, 3, 4], index=[1.1, 2.1, 3.1, 4.1])
278277
assert obj.index.dtype == np.float64
279278

@@ -306,7 +305,7 @@ def test_setitem_index_timedelta64(self):
306305
pass
307306

308307
def test_setitem_index_period(self):
309-
pass
308+
pass
310309

311310

312311
class TestInsertIndexCoercion(CoercionBase):
@@ -355,7 +354,6 @@ def test_insert_index_float64(self, insert, coerced_val, coerced_dtype):
355354
obj = pd.Float64Index([1., 2., 3., 4.])
356355
assert obj.dtype == np.float64
357356

358-
# float + int -> int
359357
exp = pd.Index([1., coerced_val, 2., 3., 4.])
360358
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
361359

@@ -377,7 +375,7 @@ def test_insert_index_datetimes(self, fill_val, exp_dtype):
377375
if fill_val.tz:
378376
with tm.assert_raises_regex(ValueError, msg):
379377
obj.insert(1, pd.Timestamp('2012-01-01'))
380-
378+
381379
with tm.assert_raises_regex(ValueError, msg):
382380
obj.insert(1, pd.Timestamp('2012-01-01', tz='Asia/Tokyo'))
383381

@@ -422,17 +420,17 @@ def test_insert_index_period(self, insert, coerced_val, coerced_dtype):
422420
index_type = pd.Index
423421

424422
exp = index_type([pd.Period('2011-01', freq='M'),
425-
coerced_val,
426-
pd.Period('2011-02', freq='M'),
427-
pd.Period('2011-03', freq='M'),
428-
pd.Period('2011-04', freq='M')], freq='M')
423+
coerced_val,
424+
pd.Period('2011-02', freq='M'),
425+
pd.Period('2011-03', freq='M'),
426+
pd.Period('2011-04', freq='M')], freq='M')
429427
self._assert_insert_conversion(obj, insert, exp, coerced_dtype)
430428

431429
def test_insert_index_complex128(self):
432430
pass
433431

434432
def test_insert_index_bool(self):
435-
pass
433+
pass
436434

437435

438436
class TestWhereCoercion(CoercionBase):
@@ -453,7 +451,7 @@ def _assert_where_conversion(self, original, cond, values,
453451
(1.1, np.object),
454452
(1 + 1j, np.object),
455453
(True, np.object)])
456-
def test_where_object(self, klass, fill_val,exp_dtype):
454+
def test_where_object(self, klass, fill_val, exp_dtype):
457455
obj = klass(list('abcd'))
458456
assert obj.dtype == np.object
459457
cond = klass([True, False, True, False])
@@ -469,8 +467,8 @@ def test_where_object(self, klass, fill_val,exp_dtype):
469467
if fill_val is True:
470468
values = klass([True, False, True, True])
471469
else:
472-
values = klass(fill_val*x for x in [5, 6, 7, 8])
473-
470+
values = klass(fill_val * x for x in [5, 6, 7, 8])
471+
474472
exp = klass(['a', values[1], 'c', values[3]])
475473
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
476474

@@ -494,7 +492,7 @@ def test_where_int64(self, klass, fill_val, exp_dtype):
494492
if fill_val is True:
495493
values = klass([True, False, True, True])
496494
else:
497-
values = klass(x*fill_val for x in [5, 6, 7, 8])
495+
values = klass(x * fill_val for x in [5, 6, 7, 8])
498496
exp = klass([1, values[1], 3, values[3]])
499497
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
500498

@@ -507,7 +505,7 @@ def test_where_int64(self, klass, fill_val, exp_dtype):
507505
(True, np.object)])
508506
def test_where_float64(self, klass, fill_val, exp_dtype):
509507
if klass is pd.Index and exp_dtype is np.complex128:
510-
pytest.skip("Complex Index not supported")
508+
pytest.skip("Complex Index not supported")
511509
obj = klass([1.1, 2.2, 3.3, 4.4])
512510
assert obj.dtype == np.float64
513511
cond = klass([True, False, True, False])
@@ -518,7 +516,7 @@ def test_where_float64(self, klass, fill_val, exp_dtype):
518516
if fill_val is True:
519517
values = klass([True, False, True, True])
520518
else:
521-
values = klass(x*fill_val for x in [5, 6, 7, 8])
519+
values = klass(x * fill_val for x in [5, 6, 7, 8])
522520
exp = klass([1.1, values[1], 3.3, values[3]])
523521
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
524522

@@ -532,14 +530,13 @@ def test_where_series_complex128(self, fill_val, exp_dtype):
532530
assert obj.dtype == np.complex128
533531
cond = pd.Series([True, False, True, False])
534532

535-
# complex + int -> complex
536533
exp = pd.Series([1 + 1j, fill_val, 3 + 3j, fill_val])
537534
self._assert_where_conversion(obj, cond, fill_val, exp, exp_dtype)
538535

539536
if fill_val is True:
540537
values = pd.Series([True, False, True, True])
541538
else:
542-
values = pd.Series(x*fill_val for x in [5, 6, 7, 8])
539+
values = pd.Series(x * fill_val for x in [5, 6, 7, 8])
543540
exp = pd.Series([1 + 1j, values[1], 3 + 3j, values[3]])
544541
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
545542

@@ -560,14 +557,14 @@ def test_where_series_bool(self, fill_val, exp_dtype):
560557
if fill_val is True:
561558
values = pd.Series([True, False, True, True])
562559
else:
563-
values = pd.Series(x*fill_val for x in [5, 6, 7, 8])
560+
values = pd.Series(x * fill_val for x in [5, 6, 7, 8])
564561
exp = pd.Series([True, values[1], True, values[3]])
565562
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
566563

567564
@pytest.mark.parametrize("fill_val,exp_dtype", [
568565
(pd.Timestamp('2012-01-01'), 'datetime64[ns]'),
569566
(pd.Timestamp('2012-01-01', tz='US/Eastern'), np.object)],
570-
ids=['datetime64', 'datetime64tz'])
567+
ids=['datetime64', 'datetime64tz'])
571568
def test_where_series_datetime64(self, fill_val, exp_dtype):
572569
obj = pd.Series([pd.Timestamp('2011-01-01'),
573570
pd.Timestamp('2011-01-02'),
@@ -586,7 +583,8 @@ def test_where_series_datetime64(self, fill_val, exp_dtype):
586583
pd.Timestamp('2012-01-02 05:00'),
587584
pd.Timestamp('2011-01-03'),
588585
pd.Timestamp('2012-01-04 05:00')])
589-
self._assert_where_conversion(obj, cond, values, exp, 'datetime64[ns]')
586+
self._assert_where_conversion(obj, cond, values, exp,
587+
'datetime64[ns]')
590588
pytest.xfail("ToDo: do not coerce to UTC, must be object")
591589

592590
exp = pd.Series([pd.Timestamp('2011-01-01'), values[1],
@@ -596,7 +594,7 @@ def test_where_series_datetime64(self, fill_val, exp_dtype):
596594
@pytest.mark.parametrize("fill_val,exp_dtype", [
597595
(pd.Timestamp('2012-01-01'), 'datetime64[ns]'),
598596
(pd.Timestamp('2012-01-01', tz='US/Eastern'), np.object)],
599-
ids=['datetime64', 'datetime64tz'])
597+
ids=['datetime64', 'datetime64tz'])
600598
def test_where_index_datetime(self, fill_val, exp_dtype):
601599
obj = pd.Index([pd.Timestamp('2011-01-01'),
602600
pd.Timestamp('2011-01-02'),
@@ -617,10 +615,12 @@ def test_where_index_datetime(self, fill_val, exp_dtype):
617615
pd.Timestamp('2012-01-04')])
618616

619617
if fill_val.tz:
620-
self._assert_where_conversion(obj, cond, values, exp, 'datetime64[ns]')
618+
self._assert_where_conversion(obj, cond, values, exp,
619+
'datetime64[ns]')
621620
pytest.xfail("ToDo: do not ignore timezone, must be object")
622621
self._assert_where_conversion(obj, cond, values, exp, exp_dtype)
623-
pytest.xfail("datetime64 + datetime64 -> datetime64 must support scalar")
622+
pytest.xfail("datetime64 + datetime64 -> datetime64 must support"
623+
" scalar")
624624

625625
def test_where_index_complex128(self):
626626
pass
@@ -644,7 +644,7 @@ def test_where_index_timedelta64(self):
644644
pass
645645

646646
def test_where_index_period(self):
647-
pass
647+
pass
648648

649649

650650
class TestFillnaSeriesCoercion(CoercionBase):
@@ -714,8 +714,7 @@ def test_fillna_series_complex128(self, fill_val, fill_dtype):
714714
(pd.Timestamp('2012-01-01'), 'datetime64[ns]'),
715715
(pd.Timestamp('2012-01-01', tz='US/Eastern'), np.object),
716716
(1, np.object), ('x', np.object)],
717-
ids=['datetime64', 'datetime64tz',
718-
'object', 'object'])
717+
ids=['datetime64', 'datetime64tz', 'object', 'object'])
719718
def test_fillna_datetime(self, klass, fill_val, fill_dtype):
720719
obj = klass([pd.Timestamp('2011-01-01'),
721720
pd.NaT,
@@ -753,14 +752,12 @@ def test_fillna_datetime64tz(self, klass, fill_val, fill_dtype):
753752
self._assert_fillna_conversion(obj, fill_val, exp, fill_dtype)
754753

755754
def test_fillna_series_int64(self):
756-
# int can't hold NaN
757755
pass
758756

759757
def test_fillna_index_int64(self):
760758
pass
761759

762760
def test_fillna_series_bool(self):
763-
# bool can't hold NaN
764761
pass
765762

766763
def test_fillna_index_bool(self):
@@ -777,7 +774,7 @@ def test_fillna_index_timedelta64(self):
777774

778775
def test_fillna_index_period(self):
779776
pass
780-
777+
781778

782779
class TestReplaceSeriesCoercion(CoercionBase):
783780

@@ -805,13 +802,13 @@ class TestReplaceSeriesCoercion(CoercionBase):
805802
@pytest.mark.parametrize('how', ['dict', 'series'])
806803
@pytest.mark.parametrize('to_key', [
807804
'object', 'int64', 'float64', 'complex128', 'bool', 'datetime64[ns]',
808-
'datetime64[ns, UTC]', 'datetime64[ns, US/Eastern]', 'timedelta64[ns]'],
809-
ids=['object', 'int64', 'float64', 'complex128',
810-
'bool', 'datetime64', 'datetime64tz', 'datetime64tz',
811-
'timedelta64'])
805+
'datetime64[ns, UTC]', 'datetime64[ns, US/Eastern]', 'timedelta64[ns]'
806+
], ids=['object', 'int64', 'float64', 'complex128', 'bool',
807+
'datetime64', 'datetime64tz', 'datetime64tz', 'timedelta64'])
812808
@pytest.mark.parametrize('from_key', [
813809
'object', 'int64', 'float64', 'complex128', 'bool', 'datetime64[ns]',
814-
'datetime64[ns, UTC]', 'datetime64[ns, US/Eastern]', 'timedelta64[ns]'])
810+
'datetime64[ns, UTC]', 'datetime64[ns, US/Eastern]', 'timedelta64[ns]']
811+
)
815812
def test_replace_series(self, how, to_key, from_key):
816813
if from_key == 'bool' and how == 'series' and compat.PY3:
817814
# doesn't work in PY3, though ...dict_from_bool works fine
@@ -822,8 +819,8 @@ def test_replace_series(self, how, to_key, from_key):
822819
assert obj.dtype == from_key
823820

824821
if (from_key.startswith('datetime') and to_key.startswith('datetime')):
825-
# different tz, currently mask_missing raises SystemError
826-
return
822+
pytest.xfail("different tz, currently mask_missing "
823+
"raises SystemError")
827824

828825
if how == 'dict':
829826
replacer = dict(zip(self.rep[from_key], self.rep[to_key]))

0 commit comments

Comments
 (0)