Skip to content

Commit

Permalink
BUG: assigning multiple columns in a hierarchically indexed DataFrame.
Browse files Browse the repository at this point in the history
…close #1803
  • Loading branch information
wesm committed Dec 2, 2012
1 parent fe0d641 commit 6abbbc1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
Empty file added LICENSES/NUMPY_LICENSE
Empty file.
Empty file added LICENSES/PSF_LICENSE
Empty file.
Empty file added LICENSES/SCIPY_LICENSE
Empty file.
3 changes: 3 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pandas 0.10.0
- Use `col_space` argument as minimum column width in DataFrame.to_html (#2328)
- Fix tz-aware DatetimeIndex.to_period (#2232)
- Fix DataFrame row indexing case with MultiIndex (#2314)
- Fix to_excel exporting issues with Timestamp objects in index (#2294)
- Fixes assigning scalars and array to hierarchical column chunk (#1803)


pandas 0.9.1
============
Expand Down
28 changes: 22 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2092,14 +2092,30 @@ def _sanitize_column(self, key, value):
value = value.copy().T
else:
value = value.copy()

# Broadcasting funtimes
if key in self.columns and value.ndim == 1:
existing_piece = self[key]
if isinstance(existing_piece, DataFrame):
value = np.tile(value, (len(existing_piece.columns), 1))
else:
value = np.repeat(value, len(self.index))
if key in self.columns:
existing_column = self[key]
# special case for now
if (com.is_float_dtype(existing_column) and
com.is_integer_dtype(value)):
value = value.astype(np.float64)
existing_piece = self[key]

# transpose hack
if isinstance(existing_piece, DataFrame):
shape = (len(existing_piece.columns), len(self.index))
value = np.repeat(value, np.prod(shape)).reshape(shape)
else:
value = np.repeat(value, len(self.index))

# special case for now
if (com.is_float_dtype(existing_piece) and
com.is_integer_dtype(value)):
value = value.astype(np.float64)

else:
value = np.repeat(value, len(self.index))

return np.atleast_2d(np.asarray(value))

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ def test_frame_setitem_multi_column(self):
cp['a'] = cp['b'].values
assert_frame_equal(cp['a'], cp['b'])

#----------------------------------------
# #1803
columns = MultiIndex.from_tuples([('A', '1'), ('A', '2'), ('B', '1')])
df = DataFrame(index=[1, 3, 5], columns=columns)

# Works, but adds a column instead of updating the two existing ones
df['A'] = 0.0 # Doesn't work
self.assertTrue((df['A'].values == 0).all())

# it broadcasts
df['B', '1'] = [1, 2, 3]
df['A'] = df['B', '1']
assert_almost_equal(df['A', '1'], df['B', '1'])
assert_almost_equal(df['A', '2'], df['B', '1'])

def test_getitem_tuple_plus_slice(self):
# GH #671
df = DataFrame({'a' : range(10),
Expand Down

0 comments on commit 6abbbc1

Please sign in to comment.