Skip to content

BUG: pandas utctimetuple() fails on python3.9 (but not 37) with timezone aware timestamps #41319

@tommyjcarpenter

Description

@tommyjcarpenter
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

Python 3.9.2 (default, Feb 24 2021, 13:26:09)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> ts = pd.Timestamp("2020-11-27 16:00:00-0500", tz="US/Eastern")
>>> ts.utctimetuple()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 1095, in pandas._libs.tslibs.timestamps.Timestamp.__new__
TypeError: an integer is required

>>> ts = pd.Timestamp("2020-11-27 16:00:00", tz="US/Eastern")
>>> ts.utctimetuple()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/_libs/tslibs/timestamps.pyx", line 1095, in pandas._libs.tslibs.timestamps.Timestamp.__new__
TypeError: an integer is required

>>> ts = pd.Timestamp("2020-11-27 16:00:00")
>>> ts.utctimetuple()
time.struct_time(tm_year=2020, tm_mon=11, tm_mday=27, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=332, tm_isdst=0)
>>>

Problem description

This does not happen in python3.7 with whatever the latest pandas is for 37; it happens on 1.2.4 in python3.9.

Pandas timestamps that are timezone aware fail .utctimetuple() however the python JSON encoder calls this function; for example I originally thought this was a flask issue: pallets/flask#3993:

return jsonify(aod)
  File "/Users/tommycarpenter/Development/python-indexapi/.tox/py39/lib/python3.9/site-packages/flask/json/__init__.py", line 370, in jsonify
    dumps(data, indent=indent, separators=separators) + "\n",
  File "/Users/tommycarpenter/Development/python-indexapi/.tox/py39/lib/python3.9/site-packages/flask/json/__init__.py", line 211, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/tommycarpenter/Development/python-indexapi/.tox/py39/lib/python3.9/site-packages/flask/json/__init__.py", line 91, in default
    return http_date(o.utctimetuple())
  File "pandas/_libs/tslibs/timestamps.pyx", line 1095, in pandas._libs.tslibs.timestamps.Timestamp.__new__
TypeError: an integer is required

Expected Output

All three variants above work, and timezoning does not affect jsonifying pandas timestamps

Output of pd.show_versions()

Details

INSTALLED VERSIONS

commit : 2cb9652
python : 3.9.2.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.1
setuptools : 53.0.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.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions