Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG/API: Previously an enlargement with a mixed-dtype frame would act unlike append (related GH2578) #8176

Merged
merged 1 commit into from
Sep 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ API changes

To insert a NaN, you must explicitly use ``np.nan``. See the :ref:`docs <missing.inserting>`.

- Previously an enlargement with a mixed-dtype frame would act unlike ``.append`` which will preserve dtypes (related :issue:`2578`, :issue:`8176`):

.. ipython:: python

df = DataFrame([[True, 1],[False, 2]], columns = ["female","fitness"])
df
df.dtypes

# dtypes are now preserved
df.loc[2] = df.loc[1]
df
df.dtypes

.. _whatsnew_0150.dt:

.dt accessor
Expand Down
25 changes: 20 additions & 5 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,27 @@ def _setitem_with_indexer(self, indexer, value):
"cannot set a frame with no defined columns"
)

index = self.obj._get_axis(0)
labels = _safe_append_to_index(index, indexer)
self.obj._data = self.obj.reindex_axis(labels, 0)._data
# append a Series
if isinstance(value, Series):

value = value.reindex(index=self.obj.columns,copy=True)
value.name = indexer

# a list-list
else:

# must have conforming columns
if com.is_list_like(value):
if len(value) != len(self.obj.columns):
raise ValueError(
"cannot set a row with mismatched columns"
)

value = Series(value,index=self.obj.columns,name=indexer)

self.obj._data = self.obj.append(value)._data
self.obj._maybe_update_cacher(clear=True)
return getattr(self.obj, self.name).__setitem__(indexer,
value)
return self.obj

# set using setitem (Panel and > dims)
elif self.ndim >= 3:
Expand Down
47 changes: 37 additions & 10 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2812,7 +2812,8 @@ def f():
df.loc[1] = df.loc[2]
assert_frame_equal(df,expected)

expected = DataFrame(dict({ 'A' : [0,2,4,4], 'B' : [1,3,5,5] }),dtype='float64')
# like 2578, partial setting with dtype preservation
expected = DataFrame(dict({ 'A' : [0,2,4,4], 'B' : [1,3,5,5] }))
df = df_orig.copy()
df.loc[3] = df.loc[2]
assert_frame_equal(df,expected)
Expand Down Expand Up @@ -2864,6 +2865,41 @@ def f():
p.loc[:,:,'C'] = Series([30,32],index=p_orig.items)
assert_panel_equal(p,expected)

def test_partial_setting_mixed_dtype(self):

# in a mixed dtype environment, try to preserve dtypes
# by appending
df = DataFrame([[True, 1],[False, 2]],
columns = ["female","fitness"])

s = df.loc[1].copy()
s.name = 2
expected = df.append(s)

df.loc[2] = df.loc[1]
assert_frame_equal(df, expected)

# columns will align
df = DataFrame(columns=['A','B'])
df.loc[0] = Series(1,index=range(4))
assert_frame_equal(df,DataFrame(columns=['A','B'],index=[0]))

# columns will align
df = DataFrame(columns=['A','B'])
df.loc[0] = Series(1,index=['B'])
assert_frame_equal(df,DataFrame([[np.nan, 1]], columns=['A','B'],index=[0],dtype='float64'))

# list-like must conform
df = DataFrame(columns=['A','B'])
def f():
df.loc[0] = [1,2,3]
self.assertRaises(ValueError, f)

# these are coerced to float unavoidably (as its a list-like to begin)
df = DataFrame(columns=['A','B'])
df.loc[3] = [6,7]
assert_frame_equal(df,DataFrame([[6,7]],index=[3],columns=['A','B'],dtype='float64'))

def test_series_partial_set(self):
# partial set with new index
# Regression from GH4825
Expand Down Expand Up @@ -3013,15 +3049,6 @@ def f():
assert_frame_equal(df,DataFrame([[1]],index=['foo'],columns=[1]))
assert_frame_equal(df,df2)

df = DataFrame(columns=['A','B'])
df.loc[3] = [6,7]
assert_frame_equal(df,DataFrame([[6,7]],index=[3],columns=['A','B']))

# no label overlap
df = DataFrame(columns=['A','B'])
df.loc[0] = Series(1,index=range(4))
assert_frame_equal(df,DataFrame(columns=['A','B'],index=[0]))

# no index to start
expected = DataFrame({ 0 : Series(1,index=range(4)) },columns=['A','B',0])

Expand Down