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

datetimelike indexes add/sub zero-dim integer arrays #19013

Merged
merged 5 commits into from
Dec 31, 2017

Conversation

jbrockmendel
Copy link
Member

Setup:

dti = pd.date_range('2016-01-01', periods=3, freq='H')
one = np.array(1)

0.21.1:

>>> dti + one
DatetimeIndex(['2016-01-01 00:00:00.000000001',
               '2016-01-01 01:00:00.000000001',
               '2016-01-01 02:00:00.000000001'],
              dtype='datetime64[ns]', freq='H')
>>> dti.freq = None
>>> dti + one
DatetimeIndex(['2016-01-01 00:00:00.000000001',
               '2016-01-01 01:00:00.000000001',
               '2016-01-01 02:00:00.000000001'],
              dtype='datetime64[ns]', freq=None)

Master: (See #19011)

>>> dti + one
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/indexes/datetimelike.py", line 685, in __add__
    elif is_offsetlike(other):
  File "pandas/core/dtypes/common.py", line 294, in is_offsetlike
    elif (is_list_like(arr_or_obj) and len(arr_or_obj) and
TypeError: len() of unsized object

After

>>> dti + one
DatetimeIndex(['2016-01-01 01:00:00', '2016-01-01 02:00:00',
               '2016-01-01 03:00:00'],
              dtype='datetime64[ns]', freq='H')
>>> dti.freq = None
>>> dti + one
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/indexes/datetimelike.py", line 683, in __add__
    return self.shift(other)
  File "pandas/core/indexes/datetimelike.py", line 821, in shift
    raise ValueError("Cannot shift with no freq")
ValueError: Cannot shift with no freq

@@ -24,6 +24,22 @@
is_interval = lib.is_interval


def is_zero_dim_array(obj):
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

this already exists

Copy link
Contributor

Choose a reason for hiding this comment

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

we have item_from_zerodim. so need to reconcile this. you generally don't need to actually check, rather you just unbox.

Copy link
Member Author

Choose a reason for hiding this comment

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

where?

Copy link
Contributor

Choose a reason for hiding this comment

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

In [4]: pd.api.types.is_scalar(np.int64(1))
Out[4]: True

@jbrockmendel
Copy link
Member Author

Wow, yah, just calling other = lib.item_from_zero_dim(other) at the top of __sub__ and __add__ is way simpler.

@codecov
Copy link

codecov bot commented Dec 31, 2017

Codecov Report

Merging #19013 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19013      +/-   ##
==========================================
+ Coverage   91.55%   91.57%   +0.02%     
==========================================
  Files         150      150              
  Lines       48939    48941       +2     
==========================================
+ Hits        44805    44817      +12     
+ Misses       4134     4124      -10
Flag Coverage Δ
#multiple 89.93% <100%> (+0.02%) ⬆️
#single 41.75% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/datetimelike.py 97.05% <100%> (+0.01%) ⬆️
pandas/util/testing.py 84.74% <0%> (-0.22%) ⬇️
pandas/plotting/_converter.py 66.95% <0%> (+1.73%) ⬆️

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 0e3c797...67522b3. Read the comment docs.

@jreback jreback added Bug Timedelta Timedelta data type Datetime Datetime data dtype labels Dec 31, 2017
@jreback jreback added this to the 0.23.0 milestone Dec 31, 2017
@jreback jreback merged commit 2030a07 into pandas-dev:master Dec 31, 2017
@jreback
Copy link
Contributor

jreback commented Dec 31, 2017

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DatetimeIndex/TimedeltaIndex add/sub zero-dim arrays incorrect
2 participants