Skip to content

DataFrameGroupBy.shift Loses TimeZone in datetime64 columns #30134

Closed
@cancan101

Description

@cancan101

Code Sample, a copy-pastable example if possible

import pandas as pd
import datetime as dt
import pytz

df = pd.DataFrame({'a':[1,1], 'date':[dt.datetime.now(tz=pytz.utc), dt.datetime.now(tz=pytz.utc)]})

df.info()

df.groupby('a').shift(0).info()

Problem description

Causes loss of timezeone meaning that you cannot subtract, etc.

Type turns to datetime64[ns]

This does not happen with df.shift()

Expected Output

datetime64[ns, UTC]

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Darwin
OS-release : 18.7.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.2
pytz : 2019.2
dateutil : 2.8.0
pip : 19.0.3
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

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