Warning when constructing a pandas DataFrame with a timezone-aware column

Viewed 249

When I run the following code:

import pandas as pd

dr = pd.date_range('2020-06-22 15:00:00', '2020-06-25 15:00:00', freq='1d')
x = pd.DataFrame({'dt': dr.tz_localize('America/New_York')}, columns=['dt'])
print(x.dt)

print(pd.__version__)

I get the following warning:

pandas/core/internals/construction.py:791: FutureWarning: Converting timezone-aware 
DatetimeArray to timezone-naive ndarray with 'datetime64[ns]' dtype. In the future, this will 
return an ndarray with 'object' dtype where each element is a 'pandas.Timestamp' with the 
correct 'tz'.
    To accept the future behavior, pass 'dtype=object'.
    To keep the old behavior, pass 'dtype="datetime64[ns]"'.
  subarr = construct_1d_object_array_from_listlike(subarr)
0   2020-06-22 15:00:00-04:00
1   2020-06-23 15:00:00-04:00
2   2020-06-24 15:00:00-04:00
3   2020-06-25 15:00:00-04:00
Name: dt, dtype: datetime64[ns, America/New_York]
0.25.3

I see in the pandas release notes that "Future versions of pandas will preserve the timezone information by returning an object-dtype NumPy array where each value is a Timestamp with the correct timezone attached". But I don't want an object-typed column, I want a datetime (of some kind) with the correct timezone attached at the column level.

Also, the print(x.dt) output seems to indicate that the column does preserve the timezone information. Is this a spurious warning, or is this indeed going to stop working like this in future versions of pandas?

As shown in the output, I'm using Pandas 0.25.3. Thanks.

NOTE: I've seen several other questions on SO about this same warning, but they seem to be about sorting a DataFrame, not just constructing one.

0 Answers
Related