Skip to content

BUG: PeriodIndex._shallow_copy doesn't roundtrip asi8 values correctly #24391

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

Closed
jschendel opened this issue Dec 21, 2018 · 5 comments
Closed
Labels
Blocker Blocking issue or pull request for an upcoming release Period Period data type Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@jschendel
Copy link
Member

On master:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.24.0.dev0+1340.g8c58817bd'

In [2]: pi = pd.period_range('2018-01-01', periods=3, freq='2D')

In [3]: pi
Out[3]: PeriodIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='period[2D]', freq='2D')

In [4]: pi._shallow_copy(pi.asi8, freq=pi.freq)
Out[4]: PeriodIndex(['2018-01-02', '2018-01-04', '2018-01-06'], dtype='period[2D]', freq='2D')

It appears that values are shifted forward by freq.n - 1 base frequencies, e.g. if freq='5D' then the above will result in a shift of '2018-01-01' --> '2018-01-05'.

Note that this is working on 0.23.4:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.23.4'

In [2]: pi = pd.period_range('2018-01-01', periods=3, freq='2D')

In [3]: pi
Out[3]: PeriodIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='period[2D]', freq='2D')

In [4]: pi._shallow_copy(pi.asi8, freq=pi.freq)
Out[4]: PeriodIndex(['2018-01-01', '2018-01-03', '2018-01-05'], dtype='period[2D]', freq='2D')

cc @jbrockmendel : Am I correct in expecting that Out[3] and Out[4] should be the same?

@jschendel jschendel added Regression Functionality that used to work in a prior pandas version Period Period data type labels Dec 21, 2018
@jbrockmendel
Copy link
Member

Definitely weird. @TomAugspurger has worked on this more recently than me. I'll try to take a look over the weekend.

@jschendel
Copy link
Member Author

FYI there is a test that's being xfailed due to this issue, so can remove it when someone has a change to look into this:

@pytest.mark.parametrize('index', [
pd.period_range('2000-01-01', periods=3, freq='D'),
pytest.param(
pd.period_range('2001-01-01', periods=3, freq='2D'),
marks=pytest.mark.xfail(reason='GH 24391')),
pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')])
def test_repeat_freqstr(self, index, use_numpy):

@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Dec 26, 2018
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Dec 26, 2018

The bug seems to be in asfreq

(Pdb) values
<PeriodArray>
['2018-01-01', '2018-01-03', '2018-01-05']
Length: 3, dtype: period[2D]
(Pdb) values.asfreq(values.freq)
<PeriodArray>
['2018-01-02', '2018-01-04', '2018-01-06']
Length: 3, dtype: period[2D]

Not so much a bug, more how we're calling it. The how='E' means we align the end, but I guess we want start in this case.

@TomAugspurger
Copy link
Contributor

I don't really understand why _shallow_copy would be calling asfreq in the first place. Ideally the PeriodArray would have the correct freq.

@TomAugspurger TomAugspurger added the Blocker Blocking issue or pull request for an upcoming release label Dec 27, 2018
@TomAugspurger
Copy link
Contributor

I think the solution is to not pass freq to _shallow_copy.

I don't plan to work on this until DatetimeArray / TimedeltaArray (#24024) are in. I think it'll be easier to fix once those are all consistent.

TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Jan 3, 2019
TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker Blocking issue or pull request for an upcoming release Period Period data type Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

3 participants