Skip to content

Bug when adding a timedelta to a datetime #6808

Closed
@adamdivak

Description

@adamdivak

Hello,

I wanted to add a specified offset (10 seconds) to and existing datetime (an index previously created by pd.to_datetime), so I thought creating a timedelta and adding it would do the trick. As you can see it doesn't yield an error but produces a rather strange results.

@buddha[J:T26]|5> base = pd.to_datetime(datetime.datetime.now())
@buddha[J:T26]|6> offset = pd.to_timedelta(10, unit='s')
@buddha[J:T26]|7> offset
              <7> numpy.timedelta64(10000000000,'ns')
@buddha[J:T26]|8> base
              <8> Timestamp('2014-04-05 13:13:40.374000', tz=None)
@buddha[J:T26]|9> base + offset
              <9> Timestamp('2014-04-05 13:13:41.784065408', tz=None)

If I try to do the same by creating a datetime.timedelta then everything works as expected.

@buddha[J:T26]|10> offset2 = datetime.timedelta(seconds = 10)
@buddha[J:T26]|11> base + offset2
              <11> Timestamp('2014-04-05 13:13:50.374000', tz=None)

This is really confusing and hard to spot. I think that pd.to_datetime and pd.to_timedelta should be compatible, but if they are not, please make the error easier to spot.

Thanks,
Adam

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions