Skip to content

Commit

Permalink
BUG: fix Panel slice setting issue and matplotlib import issues panda…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Jun 29, 2012
1 parent acb5f03 commit 76ac02e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
15 changes: 15 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ def _convert_tuple(self, key):
return tuple(keyidx)

def _setitem_with_indexer(self, indexer, value):
from pandas.core.frame import DataFrame

# also has the side effect of consolidating in-place

# mmm, spaghetti

if self.obj._is_mixed_type:
if not isinstance(indexer, tuple):
indexer = self._tuplify(indexer)
Expand All @@ -89,6 +94,10 @@ def _setitem_with_indexer(self, indexer, value):
plane_indexer = indexer[:het_axis] + indexer[het_axis + 1:]
item_labels = self.obj._get_axis(het_axis)

if isinstance(value, (np.ndarray, DataFrame)) and value.ndim > 1:
raise ValueError('Setting mixed-type DataFrames with '
'array/DataFrame pieces not yet supported')

try:
for item in item_labels[het_idx]:
data = self.obj[item]
Expand All @@ -100,6 +109,12 @@ def _setitem_with_indexer(self, indexer, value):
else:
if isinstance(indexer, tuple):
indexer = _maybe_convert_ix(*indexer)

if isinstance(value, DataFrame):
value = value.values
if not isinstance(self.obj, DataFrame):
value = value.T

self.obj.values[indexer] = value

def _getitem_tuple(self, tup):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,17 @@ def test_setitem_single_column_mixed(self):
expected = [nan, 'qux', nan, 'qux', nan]
assert_almost_equal(df['str'].values, expected)

def test_setitem_frame(self):
piece = self.frame.ix[:2, ['A', 'B']]
self.frame.ix[-2:, ['A', 'B']] = piece
assert_almost_equal(self.frame.ix[-2:, ['A', 'B']].values,
piece.values)

piece = self.mixed_frame.ix[:2, ['A', 'B']]
f = self.mixed_frame.ix.__setitem__
key = (slice(-2, None), ['A', 'B'])
self.assertRaises(ValueError, f, key, piece)

def test_setitem_fancy_exceptions(self):
pass

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ def test_getitem_fancy_ints(self):
def test_getitem_fancy_xs(self):
p = self.panel
item = 'ItemB'

date = p.major_axis[5]
col = 'C'

Expand Down Expand Up @@ -565,6 +566,16 @@ def test_getitem_fancy_xs_check_view(self):
self._check_view((item, NS, 'C'), comp)
self._check_view((NS, date, 'C'), comp)

def test_ix_setitem_slice_dataframe(self):
a = Panel(items=[1,2,3],major_axis=[11,22,33],minor_axis=[111,222,333])
b = DataFrame(np.random.randn(2,3), index=[111,333],
columns=[1,2,3])

a.ix[:, 22, [111, 333]] = b

assert_frame_equal(a.ix[:, 22, [111, 333]], b)


def _check_view(self, indexer, comp):
cp = self.panel.copy()
obj = cp.ix[indexer]
Expand Down
4 changes: 1 addition & 3 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

from pandas.util.decorators import cache_readonly
import pandas.core.common as com
from pandas.core.index import Index, MultiIndex
from pandas.core.index import MultiIndex
from pandas.core.series import Series, remove_na
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.period import PeriodIndex
from pandas.tseries.frequencies import get_period_alias, get_base_alias
from pandas.tseries.offsets import DateOffset
import pandas.tseries.tools as datetools
import pandas.lib as lib

try: # mpl optional
import pandas.tseries.converter as conv
Expand Down
2 changes: 0 additions & 2 deletions pandas/tseries/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import matplotlib.units as units
import matplotlib.dates as dates
import matplotlib.cbook as cbook
from matplotlib import pylab
from matplotlib.ticker import Formatter, AutoLocator, Locator
from matplotlib.transforms import nonsingular

Expand Down

0 comments on commit 76ac02e

Please sign in to comment.