Skip to content
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

API/DEPR: 'periods' argument instead of 'n' for DatetimeIndex.shift() #22697

Merged
merged 12 commits into from
Sep 20, 2018

Conversation

arminv
Copy link
Contributor

@arminv arminv commented Sep 13, 2018

In order to be consistent with Index.shift & Series.shift, n argument was deprecated in favor of periods.

In [2]: idx = pd.DatetimeIndex(start='2014-08-01 10:00', freq='H',
   ...:                         periods=3, tz='Asia/Calcutta')
   ...:                         

In [3]: idx
Out[3]: 
DatetimeIndex(['2014-08-01 10:00:00+05:30', '2014-08-01 11:00:00+05:30',
               '2014-08-01 12:00:00+05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq='H')

In [4]: idx.shift(1, freq=None)
Out[4]: 
DatetimeIndex(['2014-08-01 11:00:00+05:30', '2014-08-01 12:00:00+05:30',
               '2014-08-01 13:00:00+05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq='H')

In [5]: idx.shift(periods=1, freq=None)
Out[5]: 
DatetimeIndex(['2014-08-01 11:00:00+05:30', '2014-08-01 12:00:00+05:30',
               '2014-08-01 13:00:00+05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq='H')

In [6]: idx.shift(n=1, freq=None)
//anaconda/envs/pandas_dev/bin/ipython:1: FutureWarning: the 'n' keyword is deprecated, use 'periods' instead
  #!//anaconda/envs/pandas_dev/bin/python
Out[6]: 
DatetimeIndex(['2014-08-01 11:00:00+05:30', '2014-08-01 12:00:00+05:30',
               '2014-08-01 13:00:00+05:30'],
              dtype='datetime64[ns, Asia/Calcutta]', freq='H')

@pep8speaks
Copy link

pep8speaks commented Sep 13, 2018

Hello @arminv! Thanks for updating the PR.

Comment last updated on September 15, 2018 at 18:58 Hours UTC

@@ -521,40 +521,52 @@ def _addsub_offset_array(self, other, op):
kwargs['freq'] = 'infer'
return type(self)(res_values, **kwargs)

def shift(self, n, freq=None):
def shift(self, periods, freq=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to deprecate periods. Your change would break people doing .shift(n=1).

This could be somewhat tricky to do... I think you may have to make the signature *args, **kwargs, and manually do the parsing

  • shift(1): no warning
  • shift(n=1): warning
  • shift(periods=1): no warning

lmk if you need help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger Thanks. I started parsing manually but couldn't make some tests pass. Then I realized there is a decorator in pandas/pandas/util/_decorators.py that I could use for deprecating n. Lmk if there is anything else to be done.

@gfyoung gfyoung added API Design Datetime Datetime data dtype Deprecate Functionality to remove in pandas labels Sep 14, 2018
@arminv arminv changed the title API: 'periods' argument instead of 'n' for DatetimeIndex.shift() API/DEPR: 'periods' argument instead of 'n' for DatetimeIndex.shift() Sep 15, 2018
doc/source/whatsnew/v0.24.0.txt Outdated Show resolved Hide resolved
pandas/tests/indexes/datetimes/test_ops.py Outdated Show resolved Hide resolved
pandas/core/arrays/datetimelike.py Show resolved Hide resolved
pandas/core/arrays/datetimelike.py Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Sep 18, 2018

Codecov Report

Merging #22697 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #22697      +/-   ##
==========================================
+ Coverage   92.17%   92.17%   +<.01%     
==========================================
  Files         169      169              
  Lines       50778    50780       +2     
==========================================
+ Hits        46807    46809       +2     
  Misses       3971     3971
Flag Coverage Δ
#multiple 90.59% <100%> (ø) ⬆️
#single 42.33% <42.85%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/generic.py 96.67% <ø> (ø) ⬆️
pandas/core/arrays/datetimelike.py 95.53% <100%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c113db...d9bbfe8. Read the comment docs.

--------
Index.shift : Shift values of Index.
DatetimeIndex.shift : Shift values of DatetimeIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I also added Index.shift here.

tm.assert_index_equal(idx.shift(periods=0), idx)
tm.assert_index_equal(idx.shift(0), idx)
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to true? If the test fails, you may need to increase the stacklevel 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger done, test passed too.

@TomAugspurger TomAugspurger merged commit 4612a82 into pandas-dev:master Sep 20, 2018
@TomAugspurger
Copy link
Contributor

Thanks!

@TomAugspurger TomAugspurger added this to the 0.24.0 milestone Sep 20, 2018
@arminv arminv deleted the periods_keyword branch September 22, 2018 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype Deprecate Functionality to remove in pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent keyword argument name for the 'periods' in pandas.Index.shift for a DatetimeIndex
5 participants