test_ix.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from warnings import catch_warnings, simplefilter
  2. import pytest
  3. from pandas.compat import lrange
  4. from pandas.errors import PerformanceWarning
  5. from pandas import DataFrame, MultiIndex
  6. from pandas.util import testing as tm
  7. @pytest.mark.filterwarnings("ignore:\\n.ix:DeprecationWarning")
  8. class TestMultiIndexIx(object):
  9. def test_frame_setitem_ix(self, multiindex_dataframe_random_data):
  10. frame = multiindex_dataframe_random_data
  11. frame.loc[('bar', 'two'), 'B'] = 5
  12. assert frame.loc[('bar', 'two'), 'B'] == 5
  13. # with integer labels
  14. df = frame.copy()
  15. df.columns = lrange(3)
  16. df.loc[('bar', 'two'), 1] = 7
  17. assert df.loc[('bar', 'two'), 1] == 7
  18. with catch_warnings(record=True):
  19. simplefilter("ignore", DeprecationWarning)
  20. df = frame.copy()
  21. df.columns = lrange(3)
  22. df.ix[('bar', 'two'), 1] = 7
  23. assert df.loc[('bar', 'two'), 1] == 7
  24. def test_ix_general(self):
  25. # ix general issues
  26. # GH 2817
  27. data = {'amount': {0: 700, 1: 600, 2: 222, 3: 333, 4: 444},
  28. 'col': {0: 3.5, 1: 3.5, 2: 4.0, 3: 4.0, 4: 4.0},
  29. 'year': {0: 2012, 1: 2011, 2: 2012, 3: 2012, 4: 2012}}
  30. df = DataFrame(data).set_index(keys=['col', 'year'])
  31. key = 4.0, 2012
  32. # emits a PerformanceWarning, ok
  33. with tm.assert_produces_warning(PerformanceWarning):
  34. tm.assert_frame_equal(df.loc[key], df.iloc[2:])
  35. # this is ok
  36. df.sort_index(inplace=True)
  37. res = df.loc[key]
  38. # col has float dtype, result should be Float64Index
  39. index = MultiIndex.from_arrays([[4.] * 3, [2012] * 3],
  40. names=['col', 'year'])
  41. expected = DataFrame({'amount': [222, 333, 444]}, index=index)
  42. tm.assert_frame_equal(res, expected)