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: .diff() function for UTC datetimes doesn't work #18578

andersonjacob opened this Issue Nov 30, 2017 · 1 comment


None yet
2 participants

andersonjacob commented Nov 30, 2017

example code

import pandas as pd
import numpy as np
import datetime as dt

start_timestamp = int(dt.datetime(year=2017, day=1, month=1).timestamp())
dr = np.arange(start_timestamp, start_timestamp+100)

df = pd.DataFrame({'ts': dr})
df['dt'] = pd.to_datetime(df['ts'], unit='s')
df['dtUTC'] = pd.to_datetime(df['ts'], unit='s', utc=True)

# you can see that the dtypes are different between 0.20.3 and 0.21

# this all will work
__ = df['dt'].diff()
__ = df[['dt']].diff()
__ = df['dtUTC'].diff()

# this will raise an IndexError in pandas==0.21 but works in pandas==0.20.3
__ = df[['dtUTC']].diff()

Problem description

This worked in pandas 0.20.3. For some reason in pandas 0.21 and greater the to_datetime() is now providing a 'datetime64[ns, UTC]' which doesn't play well with diff() when called from the DataFrame. When called from the Series it works fine.

Expected Output

I don't really have an opinion on which dtype is used. In fact the dtypes seem more "correct" in 0.21 than in 0.20.3. I just want diff() to work when using a datetime64[ns, UTC] the same way it does for a datetime64[ns] when called from a DataFrame. It works when called from a Series so this seems like a bug.

Output of pd.show_versions()


commit: None
python-bits: 64
OS: Linux
OS-release: 4.10.0-40-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 20.7.0
Cython: None
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None


This comment has been minimized.


jreback commented Dec 1, 2017

datetime64[ns, UTC] is based on a DatetimeIndex in the impl. These are always 1-d; most of our storage is actually 2-d, so these require some handling in the internals.

Happy to have a PR to fix this.

@jreback jreback added this to the Next Major Release milestone Dec 1, 2017

@jreback jreback changed the title from diff() function for UTC datetimes doesn't work to BUG: .diff() function for UTC datetimes doesn't work Dec 1, 2017

@jreback jreback modified the milestones: Next Major Release, 0.23.0 Feb 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment