-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
ENH: support .strftime for datetimelikes (closes #10086) #10110
Conversation
this works for needs tests for the same |
that makes sense I've added to both |
pls add strftime to API.rst |
@@ -694,6 +694,21 @@ def astype(self, dtype): | |||
else: # pragma: no cover | |||
raise ValueError('Cannot cast DatetimeIndex to dtype %s' % dtype) | |||
|
|||
def strftime(self, date_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.
hmm, not to make this more complicated, but maybe move to tseries/base/DatelikeOps
(new mixin), that both DatetimeIndex/PeriodIndex
, but NOT TimedeltaIndex
inherit from.
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.
I thought about something like that too, good idea for code reuse! Will update shortly
@jreback it's updated |
@jreback I added a mini section to |
@@ -1164,12 +1164,28 @@ You can also chain these types of operations: | |||
|
|||
s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern') | |||
|
|||
You can also format the datetime values with ``Series.dt.strftime``: | |||
|
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.
maybe be explict and say that this is a string
can you update for comments. also related to #10442. I want this method to be the 'primary' date-string formatting (though copy your example that you added to basics.rst and put in 0.17.0 (in a separate section, maybe datetime-string formatting or somesuch) |
@jreback updated according to your comments except for one thing: I couldn't actually find an invalid formatting string that would cause |
@@ -1238,12 +1238,28 @@ You can also chain these types of operations: | |||
|
|||
s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern') | |||
|
|||
You can also format datetime values as strings with ``Series.dt.strftime``: | |||
|
|||
.. ipython:: python |
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 u add a reference to the standard library docs for the strftime format here
@@ -32,6 +32,23 @@ Other enhancements | |||
^^^^^^^^^^^^^^^^^^ | |||
|
|||
- ``.as_blocks`` will now take a ``copy`` optional argument to return a copy of the data, default is to copy (no change in behavior from prior versions), (:issue:`9607`) | |||
- Support ``.strftime`` for datetime-likes (:issue:`10110`) | |||
|
|||
For example: |
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.
I think this all needs to be indented 2 spaces (to form a sub-block)
We also have a |
@jorisvandenbossche sure I'll check |
imask = ~mask | ||
values[imask] = np.array([u('%s') % dt for dt in values[imask]]) | ||
|
||
formatter = lambda dt: dt.strftime(date_format) if date_format else u('%s') % dt |
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.
put the if on the outside (and lambdas on the inside), then check is only done once
@jorisvandenbossche this calls |
@jreback reordered the |
ok, lgtm. ping when green. |
sure will do |
@@ -1280,12 +1280,29 @@ You can also chain these types of operations: | |||
|
|||
s.dt.tz_localize('UTC').dt.tz_convert('US/Eastern') | |||
|
|||
You can also format datetime values as strings with ``Series.dt.strftime`` which |
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.
you can use :meth:
Series.dt.strftime`` here as well, then it makes the link to the doc page
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.
although that is maybe not that important here as it is exactly the same as standard library
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.
sure will do
8b3456c
to
c9b9ad8
Compare
@jorisvandenbossche @jreback updated to return |
6c49a7e
to
b09aacc
Compare
@jorisvandenbossche @jreback it's green now, please take a look |
@@ -2362,7 +2362,7 @@ def test_map(self): | |||
f = lambda x: x.strftime('%Y%m%d') | |||
result = rng.map(f) | |||
exp = [f(x) for x in rng] | |||
self.assert_numpy_array_equal(result, exp) | |||
np.testing.assert_array_equal(result, exp) |
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.
change back. we don't use np.testing.*
directly
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.
@jreback I changed it because assert_numpy_array_equal()
actually would fail in some of the testing environments. It seems like a problem with the older numpy
version (1.8.x)
what should I change it to instead?
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.
ahh, you are not comparing numpy arrays (they are actually a list and an array), use exp = np.asarray(....)
and I think it will work, you can also try tm.assert_almost_equal(...)
scratch that first part, use tm.assert_almost_equal
; I though you were comparing the output of .strftime
which was an np.array
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.
ah cool I think tm.assert_almost_equal
works, thanks! will update shortly
@mortada couple of comments. ping when green. |
@jreback updated, please take a look |
thanks! |
closes #10086