Skip to content

BUG: Concatenating DataFrames with NaT TZ Timestamps results in incorrect dtype #23037

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

Closed
tonytao2012 opened this issue Oct 8, 2018 · 2 comments
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype

Comments

@tonytao2012
Copy link
Contributor

Code Sample, a copy-pastable example if possible

ts1 = pd.Timestamp(pd.NaT, tz='UTC')
ts2 = pd.Timestamp('2015-01-01', tz='UTC')

df1 = pd.DataFrame([[ts1]])
df2 = pd.DataFrame([[ts2]])

result = pd.concat([df1, df2])

result[0]
Out[6]: 
0                          NaT
0    2015-01-01 00:00:00+00:00
Name: 0, dtype: object

expected = pd.DataFrame([[ts1], [ts2]])

expected[0]
Out[8]: 
0                         NaT
1   2015-01-01 00:00:00+00:00
Name: 0, dtype: datetime64[ns, UTC]

Problem description

Concatenating a DataFrame containing NaT with a tz specified with another DataFrame containing an actual date with tz specified results in an object dtype instead of the expected datetime64[ns, UTC] dtype.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.24.0.dev0+708.gce1f81f8b
pytest: 3.8.2
pip: 10.0.1
setuptools: 40.2.0
Cython: 0.28.5
numpy: 1.15.2
scipy: 1.1.0
pyarrow: None
xarray: 0.10.9
IPython: 6.5.0
sphinx: 1.8.1
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.0
openpyxl: 2.5.5
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.1.1
lxml: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.12
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: 0.1.6
fastparquet: 0.1.6
pandas_gbq: None
pandas_datareader: None
gcsfs: 0.1.2

@TomAugspurger
Copy link
Contributor

I think the issue is in the DataFrame constructor?

In [48]: df1.dtypes
Out[48]:
0    datetime64[ns]
dtype: object

That said, there's no way for the constructor to infer datetime64[ns, UTC] as the correct dtype. You would need df1 = pd.DataFrame([[ts1]], dtype='datetime64[ns, UTC]')

@sinhrks sinhrks added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype labels Oct 9, 2018
@mroeschke
Copy link
Member

I think is the intended behavior.

When concating two different dataframes with different timezone information (in this case None and UTC, we aim to preserve the timezone information on both sides so the object type is expected. One can use to_datetime to get back a datetime64[ns, UTC] dtype after the operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype
Projects
None yet
Development

No branches or pull requests

4 participants