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

COMPAT: support Timestamp.timestamp() in py2.x #17329

Closed
femtotrader opened this Issue Aug 24, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@femtotrader

femtotrader commented Aug 24, 2017

Code Sample, a copy-pastable example if possible

Python 3

In [3]: pd.Timestamp('1970-01-01', tz='UTC').timestamp()
Out[3]: 0.0

Python 2.7

In [4]: pd.Timestamp('1970-01-01', tz='UTC').timestamp()

raises

AttributeError: 'Timestamp' object has no attribute 'timestamp'

Problem description

Consistency between same Pandas version (despite difference with Python version)

Expected Output

Same output for Python 2.x and Python 3 (what ever timezone is, or without timezone)

Output of pd.show_versions()

Python 3.x

INSTALLED VERSIONS

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

pandas: 0.20.1
pytest: 3.1.2
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: 0.9.5
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: 0.5.0

Python 2.7 (install using conda virtual env)

In [21]: pd.show_versions()

INSTALLED VERSIONS

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

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.13.1
scipy: None
xarray: None
IPython: 5.3.0
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None

Related to: pydata/pandas-datareader#378 (comment)

SO question: https://stackoverflow.com/questions/40473442/attributeerror-timestamp-object-has-no-attribute-timestamp

@gfyoung gfyoung added the 2/3 Compat label Aug 24, 2017

@gfyoung

This comment has been minimized.

Show comment
Hide comment
@gfyoung

gfyoung Aug 24, 2017

Member

@femtotrader : Thanks for reporting this! Indeed, this traces back to the fact that Timestamp inherits timestamp from datetime.datetime in Python 3.x, but that is not the case in Python 2.7.

Unless implementing .timestamp is easy to do, I'm not sure how we could best address this given that the incompatibility stems for a core Python library.

Member

gfyoung commented Aug 24, 2017

@femtotrader : Thanks for reporting this! Indeed, this traces back to the fact that Timestamp inherits timestamp from datetime.datetime in Python 3.x, but that is not the case in Python 2.7.

Unless implementing .timestamp is easy to do, I'm not sure how we could best address this given that the incompatibility stems for a core Python library.

@chris-b1

This comment has been minimized.

Show comment
Hide comment
@chris-b1

chris-b1 Aug 24, 2017

Contributor

I suppose we could shim this in, implementation something like this

In [116]: ts = pd.Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')

In [117]: ts.timestamp()
Out[117]: 1413043201.123456

In [118]: ts.value // 1000 / 1000000  # truncate to ms, divide to s
Out[118]: 1413043201.123456
Contributor

chris-b1 commented Aug 24, 2017

I suppose we could shim this in, implementation something like this

In [116]: ts = pd.Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')

In [117]: ts.timestamp()
Out[117]: 1413043201.123456

In [118]: ts.value // 1000 / 1000000  # truncate to ms, divide to s
Out[118]: 1413043201.123456
@femtotrader

This comment has been minimized.

Show comment
Hide comment
@femtotrader

femtotrader Aug 25, 2017

Thanks @chris-b1
For Python 2.7 it should be ts.value // 1000 / 1000000.0 because ts.value // 1000 / 1000000 returns an integer

femtotrader commented Aug 25, 2017

Thanks @chris-b1
For Python 2.7 it should be ts.value // 1000 / 1000000.0 because ts.value // 1000 / 1000000 returns an integer

@jreback jreback added this to the Next Major Release milestone Aug 25, 2017

@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Aug 25, 2017

Contributor

this would be ok.

Contributor

jreback commented Aug 25, 2017

this would be ok.

@jreback jreback changed the title from Timestamp have a timestamp method with Python 3.x not with Python 2.7 to COMPAT: support Timestamp.timestamp() in py2.x Aug 25, 2017

@femtotrader

This comment has been minimized.

Show comment
Hide comment
@femtotrader

femtotrader Aug 25, 2017

When Timestamp haven't timezone we should add an offset
See pydata/pandas-datareader#378 (comment)

femtotrader commented Aug 25, 2017

When Timestamp haven't timezone we should add an offset
See pydata/pandas-datareader#378 (comment)

@jbrockmendel

This comment has been minimized.

Show comment
Hide comment
@jbrockmendel

jbrockmendel Oct 17, 2017

Member

Went to go implement this and now I'm confused (might relate to confusion regarding #17734). Why does an offset need to be added? I thought ts.value is already "in" UTC.

Member

jbrockmendel commented Oct 17, 2017

Went to go implement this and now I'm confused (might relate to confusion regarding #17734). Why does an offset need to be added? I thought ts.value is already "in" UTC.

jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Oct 17, 2017

@jbrockmendel jbrockmendel referenced this issue Oct 17, 2017

Merged

Add timestamp method+test; closes #17329 #17906

4 of 4 tasks complete

@jreback jreback modified the milestones: Next Major Release, 0.21.1 Oct 27, 2017

jreback added a commit that referenced this issue Oct 27, 2017

wooyekim added a commit to wooyekim/pandas that referenced this issue Oct 29, 2017

Dobatymo added a commit to Dobatymo/pandas that referenced this issue Oct 30, 2017

peterpanmj added a commit to peterpanmj/pandas that referenced this issue Oct 31, 2017

No-Stream added a commit to No-Stream/pandas that referenced this issue Nov 28, 2017

TomAugspurger added a commit to TomAugspurger/pandas that referenced this issue Dec 8, 2017

TomAugspurger added a commit that referenced this issue Dec 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment