Skip to content

Commit 4efb5e9

Browse files
committed
TST/DOC: some test cleanups
1 parent 2d2f173 commit 4efb5e9

File tree

3 files changed

+51
-54
lines changed

3 files changed

+51
-54
lines changed

doc/source/whatsnew/v0.18.0.txt

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -365,70 +365,63 @@ New Behavior:
365365
Changes to dtype assignment behaviors
366366
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
367367

368-
When a DataFrame's slice is updated with a new slice of the same
369-
dtype, the dtype of the DataFrame will now remain the same.
368+
When a DataFrame's slice is updated with a new slice of the same dtype, the dtype of the DataFrame will now remain the same. (:issue:`10503`)
370369

371370
Previous Behavior:
372371

373372
.. code-block:: python
374373

375-
In [2]: df = pd.DataFrame({'a':[0, 1, 1], 'b':[100, 200, 300]}, dtype='uint32')
374+
In [5]: df = pd.DataFrame({'a': [0, 1, 1],
375+
'b': pd.Series([100, 200, 300], dtype='uint32')})
376376

377-
In [3]: df.info()
378-
<class 'pandas.core.frame.DataFrame'>
379-
RangeIndex: 3 entries, 0 to 2
380-
Data columns (total 2 columns):
381-
a 3 non-null uint32
382-
b 3 non-null uint32
383-
dtypes: uint32(2)
384-
memory usage: 96.0 bytes
377+
In [7]: df.dtypes
378+
Out[7]:
379+
a int64
380+
b uint32
381+
dtype: object
385382

386-
In [4]: ix = df['a'] == 1
383+
In [8]: ix = df['a'] == 1
387384

388-
In [5]: df.loc[ix, 'b'] = df.loc[ix, 'b']
385+
In [9]: df.loc[ix, 'b'] = df.loc[ix, 'b']
389386

390-
In [6]: df.info()
391-
<class 'pandas.core.frame.DataFrame'>
392-
RangeIndex: 3 entries, 0 to 2
393-
Data columns (total 2 columns):
394-
a 3 non-null int64
395-
b 3 non-null int64
396-
dtypes: int64(2)
387+
In [11]: df.dtypes
388+
Out[11]:
389+
a int64
390+
b int64
391+
dtype: object
397392

398393
New Behavior:
399394

400395
.. ipython:: python
401396

402-
df = pd.DataFrame({'a':[0, 1, 1], 'b':[100, 200, 300]}, dtype='uint32')
403-
df.info()
397+
df = pd.DataFrame({'a': [0, 1, 1],
398+
'b': pd.Series([100, 200, 300], dtype='uint32')})
399+
df.dtypes
404400
ix = df['a'] == 1
405401
df.loc[ix, 'b'] = df.loc[ix, 'b']
406-
df.info()
407-
402+
df.dtypes
408403

409-
When a DataFrame's integer slice is partially updated with a new slice of floats that
410-
could potentially be downcasted to integer without losing precision,
411-
the dtype of the slice will be set to float instead of integer.
404+
When a DataFrame's integer slice is partially updated with a new slice of floats that could potentially be downcasted to integer without losing precision, the dtype of the slice will be set to float instead of integer.
412405

413406
Previous Behavior:
414407

415408
.. code-block:: python
416409

417410
In [4]: df = pd.DataFrame(np.array(range(1,10)).reshape(3,3),
418-
...: columns=list('abc'),
419-
...: index=[[4,4,8], [8,10,12]])
411+
columns=list('abc'),
412+
index=[[4,4,8], [8,10,12]])
420413

421414
In [5]: df
422-
Out[5]:
415+
Out[5]:
423416
a b c
424417
4 8 1 2 3
425418
10 4 5 6
426419
8 12 7 8 9
427420

428-
In [6]: df.ix[4, 'c'] = np.array([0., 1.])
421+
In [7]: df.ix[4, 'c'] = np.array([0., 1.])
429422

430-
In [7]: df
431-
Out[7]:
423+
In [8]: df
424+
Out[8]:
432425
a b c
433426
4 8 1 2 0
434427
10 4 5 1
@@ -439,8 +432,8 @@ New Behavior:
439432
.. ipython:: python
440433

441434
df = pd.DataFrame(np.array(range(1,10)).reshape(3,3),
442-
columns=list('abc'),
443-
index=[[4,4,8], [8,10,12]])
435+
columns=list('abc'),
436+
index=[[4,4,8], [8,10,12]])
444437
df
445438
df.ix[4, 'c'] = np.array([0., 1.])
446439
df
@@ -1202,4 +1195,3 @@ Bug Fixes
12021195
- Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`)
12031196
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
12041197
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:`11934`)
1205-
- Bug when modifying a slice of a ``DataFrame`` with the same ``dtype``, the ``dtype`` of the ``DataFrame`` could unexpected changed. (:issue:`10503`).

pandas/tests/test_generic.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -362,24 +362,6 @@ def test_head_tail(self):
362362
self._compare(o.head(-3), o.head(7))
363363
self._compare(o.tail(-3), o.tail(7))
364364

365-
def test_dtype_after_slice_update(self):
366-
# GH10503
367-
368-
# assigning the same type should not change the type
369-
df1 = pd.DataFrame({'a': [0, 1, 1], 'b': [100, 200, 300]},
370-
dtype='uint32')
371-
ix = df1['a'] == 1
372-
newb1 = df1.loc[ix, 'b'] + 1
373-
df1.loc[ix, 'b'] = newb1
374-
assert_equal(df1['a'].dtype, newb1.dtype)
375-
376-
# assigning a new type should get the inferred type
377-
df2 = pd.DataFrame({'a': [0, 1, 1], 'b': [100, 200, 300]},
378-
dtype='uint64')
379-
newb2 = df2.loc[ix, 'b']
380-
df1.loc[ix, 'b'] = newb2
381-
assert_equal(df1['a'].dtype, np.dtype('int64'))
382-
383365
def test_sample(self):
384366
# Fixes issue: 2419
385367

pandas/tests/test_indexing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,29 @@ def test_iloc_setitem(self):
699699
expected = Series([0, 1, 0], index=[4, 5, 6])
700700
assert_series_equal(s, expected)
701701

702+
def test_loc_setitem_slice(self):
703+
# GH10503
704+
705+
# assigning the same type should not change the type
706+
df1 = DataFrame({'a': [0, 1, 1],
707+
'b': Series([100, 200, 300], dtype='uint32')})
708+
ix = df1['a'] == 1
709+
newb1 = df1.loc[ix, 'b'] + 1
710+
df1.loc[ix, 'b'] = newb1
711+
expected = DataFrame({'a': [0, 1, 1],
712+
'b': Series([100, 201, 301], dtype='uint32')})
713+
assert_frame_equal(df1, expected)
714+
715+
# assigning a new type should get the inferred type
716+
df2 = DataFrame({'a': [0, 1, 1], 'b': [100, 200, 300]},
717+
dtype='uint64')
718+
ix = df1['a'] == 1
719+
newb2 = df2.loc[ix, 'b']
720+
df1.loc[ix, 'b'] = newb2
721+
expected = DataFrame({'a': [0, 1, 1], 'b': [100, 200, 300]},
722+
dtype='uint64')
723+
assert_frame_equal(df2, expected)
724+
702725
def test_ix_loc_setitem_consistency(self):
703726

704727
# GH 5771

0 commit comments

Comments
 (0)