-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Fixing EX01 - Added examples #53725
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
Changes from 3 commits
ce37a6e
58f2810
a4bc631
299a31c
dac6e86
cf3a12c
68438b8
109bed1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1202,7 +1202,16 @@ cdef class _Timestamp(ABCTimestamp): | |
|
||
cpdef to_datetime64(self): | ||
""" | ||
Return a numpy.datetime64 object with 'ns' precision. | ||
Return a numpy.datetime64 object with same precision. | ||
|
||
Examples | ||
-------- | ||
>>> ts = pd.Timestamp(year=2023, month=1, day=1, | ||
... hour=10, second=15) | ||
>>> ts | ||
Timestamp('2023-01-01 10:00:15') | ||
>>> ts.to_datetime64() | ||
numpy.datetime64('2023-01-01T10:00:15.000000') | ||
""" | ||
# TODO: find a way to construct dt64 directly from _reso | ||
abbrev = npy_unit_to_abbrev(self._creso) | ||
|
@@ -1531,6 +1540,99 @@ class Timestamp(_Timestamp): | |
) from err | ||
return _dt.ctime() | ||
|
||
def time(self): | ||
""" | ||
Return time object with same time but with tzinfo=None. | ||
|
||
Examples | ||
-------- | ||
>>> ts = pd.Timestamp('2023-01-01 10:00:00') | ||
>>> ts | ||
Timestamp('2023-01-01 10:00:00') | ||
>>> ts.time() | ||
datetime.time(10, 0) | ||
""" | ||
try: | ||
_dt = datetime(self.year, self.month, self.day, | ||
self.hour, self.minute, self.second, | ||
self.microsecond, self.tzinfo, fold=self.fold) | ||
except ValueError as err: | ||
raise NotImplementedError( | ||
"time not yet supported on Timestamps which " | ||
"are outside the range of Python's standard library. " | ||
) from err | ||
return _dt.time() | ||
|
||
def timetuple(self): | ||
""" | ||
Return time tuple, compatible with time.localtime(). | ||
|
||
Examples | ||
-------- | ||
>>> ts = pd.Timestamp('2023-01-01 10:00:00') | ||
>>> ts | ||
Timestamp('2023-01-01 10:00:00') | ||
>>> ts.timetuple() | ||
time.struct_time(tm_year=2023, tm_mon=1, tm_mday=1, | ||
tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1) | ||
""" | ||
try: | ||
_dt = datetime(self.year, self.month, self.day, | ||
self.hour, self.minute, self.second, | ||
self.microsecond, self.tzinfo, fold=self.fold) | ||
except ValueError as err: | ||
raise NotImplementedError( | ||
"timetuple not yet supported on Timestamps which " | ||
"are outside the range of Python's standard library. " | ||
) from err | ||
return _dt.timetuple() | ||
|
||
def timetz(self): | ||
""" | ||
Return time object with same time and tzinfo. | ||
|
||
Examples | ||
-------- | ||
>>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels') | ||
>>> ts | ||
Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels') | ||
>>> ts.timetz() | ||
datetime.time(10, 0, tzinfo=<DstTzInfo 'Europe/Brussels' CET+1:00:00 STD>) | ||
""" | ||
try: | ||
_dt = datetime(self.year, self.month, self.day, | ||
self.hour, self.minute, self.second, | ||
self.microsecond, self.tzinfo, fold=self.fold) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above (can we use (unrelated, but I'm really confused about what the use of this method is - time zones make little sense if there aren't dates associated with them.. 😄) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I must be missing something but I get: |
||
except ValueError as err: | ||
raise NotImplementedError( | ||
"timetz not yet supported on Timestamps which " | ||
"are outside the range of Python's standard library. " | ||
) from err | ||
return _dt.timetz() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MarcoGorelli - sorry I just saw that here I'm returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh wait, sorry, there is, that's why there was no definition previously in which case, yeah, if it doesn't give incorrect results for negative dates or anything, then that looks fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to work fine In [41]: pd.Timestamp('-2000-01-01 01:00:00+01:00').timetz()
Out[41]: datetime.time(1, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600))) ok to just use this then, thanks for noticing! |
||
def toordinal(self): | ||
""" | ||
Return proleptic Gregorian ordinal. January 1 of year 1 is day 1. | ||
|
||
Examples | ||
-------- | ||
>>> ts = pd.Timestamp('2023-01-01 10:00:50') | ||
>>> ts | ||
Timestamp('2023-01-01 10:00:50') | ||
>>> ts.toordinal() | ||
738521 | ||
""" | ||
try: | ||
_dt = datetime(self.year, self.month, self.day, | ||
self.hour, self.minute, self.second, | ||
self.microsecond, self.tzinfo, fold=self.fold) | ||
except ValueError as err: | ||
raise NotImplementedError( | ||
"toordinal not yet supported on Timestamps which " | ||
"are outside the range of Python's standard library. " | ||
) from err | ||
return _dt.toordinal() | ||
|
||
# Issue 25016. | ||
@classmethod | ||
def strptime(cls, date_string, format): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we construct a
time
object directly, wheretime
is imported fromdatetime
? then it shouldn't be necessary to have try/except, it should always pass