Skip to content

Commit

Permalink
BUG: Fix Series constructor when copy=True
Browse files Browse the repository at this point in the history
Updates Series constructor to include copy argument when dtype
argument is also provided.

closes #15125

Author: Rouz Azari <[email protected]>

Closes #15128 from rouzazari/series_dtype_param_no_side_effects_when_copy_true and squashes the following commits:

7b0c959 [Rouz Azari] BUG: Fix Series constructor when copy=True
  • Loading branch information
rouzazari authored and jreback committed Jan 15, 2017
1 parent a19b0d8 commit 0e219d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Bug Fixes




- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)

- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
# create/copy the manager
if isinstance(data, SingleBlockManager):
if dtype is not None:
data = data.astype(dtype=dtype, raise_on_error=False)
data = data.astype(dtype=dtype, raise_on_error=False,
copy=copy)
elif copy:
data = data.copy()
else:
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ def test_constructor_sanitize(self):
s = Series(np.array([1., 1., np.nan]), copy=True, dtype='i8')
self.assertEqual(s.dtype, np.dtype('f8'))

def test_constructor_copy(self):
# GH15125
# test dtype parameter has no side effects on copy=True
for data in [[1.], np.array([1.])]:
x = Series(data)
y = pd.Series(x, copy=True, dtype=float)

# copy=True maintains original data in Series
tm.assert_series_equal(x, y)

# changes to origin of copy does not affect the copy
x[0] = 2.
self.assertFalse(x.equals(y))
self.assertEqual(x[0], 2.)
self.assertEqual(y[0], 1.)

def test_constructor_pass_none(self):
s = Series(None, index=lrange(5))
self.assertEqual(s.dtype, np.float64)
Expand Down

0 comments on commit 0e219d7

Please sign in to comment.