ENH/BUG: DatetimeIndex and PeriodIndex in-place ops behaves incorrectly #7741

Merged
merged 1 commit into from Jul 23, 2014

Conversation

Projects
None yet
2 participants
Member

sinhrks commented Jul 13, 2014

Fixes 2 issues related to DetetimeIndex and PeriodIndex ops.

  • Addition / subtraction between PeriodIndex raise TypeError (Closes #3367).
pidx + pidx
# TypeError: unsupported operand type(s) for +: 'PeriodIndex' and 'PeriodIndex'
  • In-place addition / subtraction doesn't return the same result as normal addition / subtraction. Specifically, PeriodIndex in-place operation results in Int64Index (Closes #6527)
didx = pd.date_range('2011-01-01', freq='D', periods=5)

# This results shift (expected)
didx + 1
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2011-01-02, ..., 2011-01-06]
# Length: 5, Freq: D, Timezone: None

# This result adding 1 unit (nano second) (NG)
didx += 1
didx 
# <class 'pandas.tseries.index.DatetimeIndex'>
# [2011-01-01 00:00:00.000000001, ..., 2011-01-05 00:00:00.000000001]
# Length: 5, Freq: None, Timezone: None

sinhrks changed the title from ENH: PeriodIndex ops can accept Index to ENH/BUG: DatetimeIndex and PeriodIndex in-place ops behaves incorrectly Jul 13, 2014

@jreback jreback modified the milestone: 0.15.1, 0.15.0 Jul 14, 2014

Member

sinhrks commented Jul 19, 2014

This can solve #3367. Test cases are added.

@jreback jreback and 1 other commented on an outdated diff Jul 19, 2014

pandas/tests/test_base.py
@@ -729,6 +825,86 @@ def test_representation(self):
result = getattr(idx, func)()
self.assertEqual(result, expected)
+ def test_add_iadd(self):
+ # union
@jreback

jreback Jul 19, 2014

Contributor

can you add a couple of tests that have Periods with another freq as well?

@sinhrks

sinhrks Jul 19, 2014

Member

OK, added few more test both for addition and subtraction. There seems to be a problem for non-monotonic PeriodIndex.union and diff. I'll dig in...

@jreback

jreback Jul 21, 2014

Contributor

I meant like adding 'M' (higher than day freq). lmk

@sinhrks

sinhrks Jul 23, 2014

Member

OK. Added tests for 'M' and 'A'.

Contributor

jreback commented Jul 23, 2014

look good..thanks

@jreback jreback added a commit that referenced this pull request Jul 23, 2014

@jreback jreback Merge pull request #7741 from sinhrks/period_ops
ENH/BUG: DatetimeIndex and PeriodIndex in-place ops behaves incorrectly
492605e

@jreback jreback merged commit 492605e into pandas-dev:master Jul 23, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details

sinhrks deleted the sinhrks:period_ops branch Jul 25, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment