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

core: fix DatetimeBlock operated with timedelta #22007

Closed
wants to merge 2 commits into from

Conversation

holymonson
Copy link
Contributor

@holymonson holymonson commented Jul 21, 2018

@holymonson holymonson force-pushed the frame-add-timedelta branch from effc448 to 66da338 Compare July 21, 2018 06:05
@codecov
Copy link

codecov bot commented Jul 21, 2018

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #22007      +/-   ##
==========================================
+ Coverage   92.02%   92.02%   +<.01%     
==========================================
  Files         170      170              
  Lines       50710    50715       +5     
==========================================
+ Hits        46664    46669       +5     
  Misses       4046     4046
Flag Coverage Δ
#multiple 90.43% <100%> (ø) ⬆️
#single 42.3% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/internals/blocks.py 94.47% <100%> (+0.01%) ⬆️

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 2c7c797...518b3aa. Read the comment docs.

@gfyoung gfyoung added Datetime Datetime data dtype Timedelta Timedelta data type labels Jul 23, 2018
@gfyoung gfyoung requested review from WillAyd and jreback and removed request for WillAyd July 23, 2018 04:59
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other
tm.assert_frame_equal(result, expected)

Copy link
Member

Choose a reason for hiding this comment

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

Can you reference the issue as a comment under the function definition.

@holymonson holymonson force-pushed the frame-add-timedelta branch from 66da338 to 057a8f3 Compare July 23, 2018 05:03
@TomAugspurger
Copy link
Contributor

cc @jbrockmendel

])
def test_timestamp_df_add_timedelta(self, other):
# GH 22005
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add another test that uses a dataframe of mixed types (e.g. just add a string column and an integer column)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

int or str can't operate with Timedelta. Even if they are mixed in one DataFrame, they use different Block, won't be affect by DatetimeBlock here.

pd.Timedelta('1d') + pd.DataFrame([pd.Timestamp('2018-01-01'), 123])
TypeError: Could not operate Timedelta('1 days 00:00:00') with block values unsupported operand type(s) for +: 'Timedelta' and 'int'

Copy link
Member

Choose a reason for hiding this comment

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

int or str can't operate with Timedelta.

The idea is 1) for the mixed-dtype cases that are valid, test them, 2) for (a few of) the mixed-dtype cases that are not, test that they raise as expected.

def test_timestamp_df_add_timedelta(self, other):
# GH 22005
expected = pd.DataFrame([pd.Timestamp('2018-01-02')])
result = pd.DataFrame([pd.Timestamp('2018-01-01')]) + other
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the reverse test as well (other + DataFrame...)

Copy link
Member

Choose a reason for hiding this comment

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

While you're at it, might as well do subtraction too

@@ -2725,6 +2725,11 @@ def _try_coerce_args(self, values, other):
"naive Block")
other_mask = isna(other)
other = other.asm8.view('i8')
elif isinstance(other, (Timedelta, timedelta, np.timedelta64)):
# should be the same as TimeDeltaBlock._box_func
other = Timedelta(other, unit='ns')
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the unit

Copy link
Member

Choose a reason for hiding this comment

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

Also don't need to check for Timedelta, since it subclasses timedelta.

@jbrockmendel
Copy link
Member

Pending requested fleshing out in tests (and rebase), this looks like a fine stop-gap solution until we rip out Block-based arithmetic altogether.

@holymonson holymonson force-pushed the frame-add-timedelta branch from 057a8f3 to 518b3aa Compare July 26, 2018 03:39
@jbrockmendel
Copy link
Member

The issue #22005 this was written for was closed by #22163. @holymonson are there other bugs this fixes?

@holymonson
Copy link
Contributor Author

The issue #22005 this was written for was closed by #22163. @holymonson are there other bugs this fixes?

Nope, I will close this.

@holymonson holymonson closed this Aug 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataFrame(Timestamp) add Timedelta fails
5 participants