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

Bug when adding a timedelta to a datetime #6808

Closed
adamdivak opened this issue Apr 5, 2014 · 6 comments
Closed

Bug when adding a timedelta to a datetime #6808

adamdivak opened this issue Apr 5, 2014 · 6 comments
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@adamdivak
Copy link

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

@jreback
Copy link
Contributor

jreback commented Apr 5, 2014

pandas/numpy version?

@jreback
Copy link
Contributor

jreback commented Apr 5, 2014

pandas 0.13.0 (and 0.13.1) / numpy 1.7.1

In [17]: base = pd.to_datetime(datetime.datetime.now())

In [18]: base
Out[18]: Timestamp('2014-04-05 09:55:36.107706', tz=None)

In [19]: offset = pd.to_timedelta(10, unit='s')

In [20]: offset
Out[20]: numpy.timedelta64(10000000000,'ns')

In [21]: offset2 = datetime.timedelta(seconds = 10)

In [22]: offset2
Out[22]: datetime.timedelta(0, 10)

In [23]: base+offset
Out[23]: Timestamp('2014-04-05 09:55:46.107706', tz=None)

In [24]: base+offset2
Out[24]: Timestamp('2014-04-05 09:55:46.107706', tz=None)

@adamdivak
Copy link
Author

I'm sorry, forgot to include. Do you think this could be due to the newer numpy version?

commit: None
python: 2.7.6.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: x86
processor: x86 Family 6 Model 23 Stepping 10, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.13.1
Cython: None
numpy: 1.8.0
scipy: 0.13.3
statsmodels: 0.5.0
IPython: 1.2.0
sphinx: 1.2.1
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.2
pytz: 2013.9
bottleneck: 0.8.0
tables: 3.1.0
numexpr: 2.3
matplotlib: 1.3.1
openpyxl: None
xlrd: 0.9.2
xlwt: 0.7.5
xlsxwriter: None
sqlalchemy: 0.8.4
lxml: 3.3.1
bs4: 4.3.2
html5lib: 0.999
bq: None
apiclient: None

@jreback
Copy link
Contributor

jreback commented Apr 5, 2014

odd....32-bit python messes this up (must be needs to be explicity converted somewhere).

the reason is by default creating an array on 32-bit gives you 32-bit ints, while we use 64-bit ints everywhere

thanks for the report

I guess don't have an explicit test for this

@jreback
Copy link
Contributor

jreback commented Apr 5, 2014

closed via b4f3f0f

I will post updated binaries here soon: http://pandas.pydata.org/pandas-build/dev/

and this will be in 0.14 thanks for the report

@jreback jreback closed this as completed Apr 5, 2014
@adamdivak
Copy link
Author

Thanks for the quick fix.

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

Successfully merging a pull request may close this issue.

2 participants