Skip to content

Difference between two Timestamp with different timezone object type fails #31793

Closed
@antoine-gallix

Description

@antoine-gallix

I encountered an issue while calculating the difference between two timestamps
of different origin. The first timestamp comes from a function using the arrow
package. The timestamp is in UTC timezone. The other timestamp comes for another
package returning a pandas.DataFrame, also with UTC timestamps. An exception is
raised when trying to get the difference between the two timestamps. Here follow
some come that reproduces the error, with some details about the respective
time zones.

import arrow
import pandas


def info(name, thing):
    print(f"{name} | tzinfo: {thing.tzinfo} | tzinfo type: {type(thing.tzinfo)}")


arrow_now = arrow.utcnow()
info("arrow.utcnow()", arrow_now)
arrow_now_datetime = arrow_now.datetime
info("arrow.utcnow().datetime", arrow_now_datetime)
timestamp_from_datetime_from_arrow_now = pandas.Timestamp(arrow_now_datetime)
info(
    "pandas.Timestamp(arrow.utcnow().datetime)", timestamp_from_datetime_from_arrow_now
)
pandas_now_timestamp = pandas.Timestamp.utcnow()
info("pandas.Timestamp.utcnow()", pandas_now_timestamp)

Running the above code produces:

arrow.utcnow() | tzinfo: tzutc() | tzinfo type: <class 'dateutil.tz.tz.tzutc'>
arrow.utcnow().datetime | tzinfo: tzutc() | tzinfo type: <class 'dateutil.tz.tz.tzutc'>
pandas.Timestamp(arrow.utcnow().datetime) | tzinfo: tzutc() | tzinfo type: <class 'dateutil.tz.tz.tzutc'>
pandas.Timestamp.utcnow() | tzinfo: UTC | tzinfo type: <class 'pytz.UTC'>

Both timestamps are in the same timezone, but the timezone object itself comes from different packages. Now when trying to get the difference between the two timestamps:

timestamp_from_datetime_from_arrow_now - pandas_now_timestamp

I get the following error:

TypeError: Timestamp subtraction must have the same timezones or no timezones

I would expect that if the Timestamp objects are initialized without error,
which is the case, that they are valid object that can work with each other,
independently from what were the timestamps build with. It's not the case.

System Info

  • system: Ubuntu 19.04 disco
  • python version: 3.7.4
  • arrow version: 0.15.5
  • pandas version: 1.0.0

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.0.0-38-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 6.0.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
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 1.3.13
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCompatpandas objects compatability with Numpy or Python functionsNumeric OperationsArithmetic, Comparison, and Logical operationsTimezonesTimezone data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions