-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: astype(str) on datetimelike index #10442 #11148
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
Conversation
import pandas as pd | ||
pd.date_range('2012-01-01', periods=4).astype(str) | ||
result = ['2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04'] | ||
assert(type(result[0]) == np.str) |
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.
The test should compare the result and expected as below. I understand the result will be np.object
dtype, not numpy.str
.
result = pd.date_range('2012-01-01', periods=4).astype(str)
expected = pd.Index(['2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04'], dtype=object)
tm.assert_index_equal(result, expected)
Also, can you add other test cases, such as different freq like H
, and with timezone.
I added all the tests..please let me know if I need to add anything else. |
|
||
|
||
def test_datetimelike_tostring_astype_freqH(): | ||
import pandas as pd |
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 don't need any of the imports, though you may need _skip_if_no_pytz
. below
sounds fine now, let me know if I need to add anythingelse |
@@ -504,6 +504,29 @@ def test_infer_freq(self): | |||
self.assertEqual(result.freq, freq) | |||
|
|||
|
|||
def test_datetimelike_tostring_astype(): |
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.
these should be under the TestDatetimeIndexOps
class
pls add a note in whatsnew for 0.17.1 (enhancements). squash as well. |
going to need tests for |
@jreback |
rebase on master |
tm.assert_index_equal(result, expected) | ||
|
||
# test astype string with freqH and timezone | ||
import pytz |
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.
add a _skip_if_no_pytz
@hamedhsn can you update |
@jreback sorry, what do I need to update? |
comments above, needs a rebase as well |
@jreback did that. |
@@ -17,6 +17,7 @@ Highlights include: | |||
|
|||
Enhancements | |||
~~~~~~~~~~~~ | |||
- adding conversion of ``DatetimeIndex`` values to strings using ``astype(str)`` (:issue:`10442`) |
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.
say DatetimeIndex
now supports conversion to strings with astype(str)
couple of comments. pls squash as well |
@jreback sorry that I am asking..how can I squash the last few commits to one. I tried a few different approaches without a luck. |
@jreback squashed all the commits into one. |
you need to make sure you force push too.
squash (put a s by each commit)
|
@jreback did that. thanks |
result = ['2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04'] | ||
assert(type(result[0]) == np.str) | ||
|
||
|
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.
the above test doesn't do anything. pls make it more like @sinhrks suggested
@jreback should be fine now. |
ok @hamedhsn ping on green. |
@jreback strange, not sure why test is failed. both sides are object type but it raises this error.. I tried the test with both side same nothing to do with astype but still get the same error |
# BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex | ||
result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str) | ||
expected = pd.Series(['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object) | ||
tm.assert_index_equal(result, expected) |
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 need to use tm.assert_series_equal
here (and below)
BUG pandas-dev#10442(test) : Convert datetimelike index to strings with astype(str) BUG#10422: note added bug#10442 : tests added bug#10442 : note udated BUG pandas-dev#10442(test) : Convert datetimelike index to strings with astype(str) bug#10442: fix, adding note and test bug#10442: fix, adding note and test
@jreback it is fine now. |
BUG: astype(str) on datetimelike index #10442
thanks! |
closes #10442
fix for the bug: Convert datetimelike index to strings with astype(str)
going to send pull request for test