Skip to content
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

Add timestamp method+test; closes #17329 #17906

Merged
merged 17 commits into from
Oct 27, 2017

Conversation

jbrockmendel
Copy link
Member

@@ -235,6 +235,7 @@ Other Enhancements
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
- :func:`read_json` and :func:`to_json` now accept a ``compression`` argument which allows them to transparently handle compressed files. (:issue:`17798`)
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (:issue:`17367`)
- :meth:`Timestamp.timestamp` is now available in python 2.7. (:issue:`17329`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: All other instances of "Python" in the whatsnew have a capitalized P.

@@ -1405,6 +1404,10 @@ cdef class _Timestamp(datetime):
def __get__(self):
return np.datetime64(self.value, 'ns')

def timestamp(self):
# py27 compat, see GH#17329
return self.value / 1e9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a doc-string

def test_timestamp(self):
# Gh#17329
# tz-naive --> treat it as if it were UTC for purposes of timestamp()
ts = Timestamp.now()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test versus a datetime.timestamp() value here (as well)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. ts.to_pydatetime().timestamp()

@jreback jreback added 2/3 Compat Compat pandas objects compatability with Numpy or Python functions Datetime Datetime data dtype labels Oct 17, 2017
@codecov
Copy link

codecov bot commented Oct 17, 2017

Codecov Report

Merging #17906 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17906      +/-   ##
==========================================
- Coverage   91.23%   91.22%   -0.02%     
==========================================
  Files         163      163              
  Lines       50105    50105              
==========================================
- Hits        45715    45706       -9     
- Misses       4390     4399       +9
Flag Coverage Δ
#multiple 89.03% <ø> (ø) ⬆️
#single 40.31% <ø> (-0.06%) ⬇️
Impacted Files Coverage Δ
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.75% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5bf7f9a...a88419d. Read the comment docs.

@codecov
Copy link

codecov bot commented Oct 17, 2017

Codecov Report

Merging #17906 into master will increase coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17906      +/-   ##
==========================================
+ Coverage   91.24%   91.25%   +0.01%     
==========================================
  Files         163      163              
  Lines       50173    50173              
==========================================
+ Hits        45778    45786       +8     
+ Misses       4395     4387       -8
Flag Coverage Δ
#multiple 89.06% <ø> (+0.03%) ⬆️
#single 40.29% <ø> (-0.05%) ⬇️
Impacted Files Coverage Δ
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.75% <0%> (-0.1%) ⬇️
pandas/plotting/_converter.py 65.2% <0%> (+1.81%) ⬆️
pandas/io/msgpack/_version.py 44.65% <0%> (+1.9%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc1e507...7992462. Read the comment docs.

@@ -235,6 +235,7 @@ Other Enhancements
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
- :func:`read_json` and :func:`to_json` now accept a ``compression`` argument which allows them to transparently handle compressed files. (:issue:`17798`)
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (:issue:`17367`)
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this in api.rst?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now.

# utsc is a different representation of the same time
assert tsc.timestamp() == utsc.timestamp()

if PY3:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not available in py2? on datetime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. The method was added to the stdlib datetime.datetime in py3.3

uts = ts.replace(tzinfo=utc)
assert ts.timestamp() == uts.timestamp()

tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u maker sure test for mat is ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for NaT

dt = ts.to_pydatetime()
assert dt.timestamp() == round(ts.timestamp(), 6)

pytest.raises(ValueError, NaT.timestamp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to the section where we test NaT (test_nat.py)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come to think of it, should NaT.timestamp raise or return nan?

if PY3:
# should agree with datetime.timestamp method
dt = ts.to_pydatetime()
assert dt.timestamp() == round(ts.timestamp(), 6)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to round?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because dt.timestamp() is the datetime.timestamp method which only has microsecond-precision.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, you should not need to do this

def timestamp(self):
"""Return POSIX timestamp as float."""
# py27 compat, see GH#17329
return self.value / 1e9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is wrong if we have a nanosecond component. should issue a warning if we have one (I think its a print statement, but that's consistent with what we have now). add a test for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it wrong with a nanosecond component? Is it a precision issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes its not compatible with datetime.timestamp() (since you cannot construct one in datetime, we should not allow a time value which is invalid)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make this change, will add follow-up to the tslibs todo list, since it isn't obvious to me that this is the correct decision.

@bkmrkr
Copy link

bkmrkr commented Oct 21, 2017 via email

@@ -123,6 +123,11 @@ def test_round_nat(klass):
assert round_method(freq) is ts


def test_timestamp():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls just add this where the others are (IOW in the list), don't special case this.

@jbrockmendel jbrockmendel mentioned this pull request Oct 21, 2017
59 tasks
@jreback
Copy link
Contributor

jreback commented Oct 21, 2017

try. test where u take a min zero nanosecond Timestamp then do .timestamp() and create this as a datetime it will lose the nanoseconds

@jreback jreback added this to the 0.21.1 milestone Oct 27, 2017
@jreback jreback merged commit 5dd2ea0 into pandas-dev:master Oct 27, 2017
@jreback
Copy link
Contributor

jreback commented Oct 27, 2017

thanks

@jbrockmendel jbrockmendel deleted the tslibs-timestamps branch October 27, 2017 22:09
peterpanmj pushed a commit to peterpanmj/pandas that referenced this pull request Oct 31, 2017
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request Nov 28, 2017
TomAugspurger pushed a commit to TomAugspurger/pandas that referenced this pull request Dec 8, 2017
TomAugspurger pushed a commit that referenced this pull request Dec 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

COMPAT: support Timestamp.timestamp() in py2.x
5 participants