Skip to content

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

Merged
merged 1 commit into from
Oct 16, 2015

Conversation

hamedhsn
Copy link

closes #10442

fix for the bug: Convert datetimelike index to strings with astype(str)
going to send pull request for test

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)
Copy link
Member

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.

@sinhrks sinhrks added the Dtype Conversions Unexpected or buggy dtype conversions label Sep 19, 2015
@sinhrks sinhrks added this to the 0.17.1 milestone Sep 19, 2015
@hamedhsn
Copy link
Author

I added all the tests..please let me know if I need to add anything else.

@jreback jreback removed this from the 0.17.1 milestone Sep 20, 2015
@jreback jreback added Datetime Datetime data dtype Output-Formatting __repr__ of pandas objects, to_string labels Sep 20, 2015


def test_datetimelike_tostring_astype_freqH():
import pandas as pd
Copy link
Contributor

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

@jreback jreback changed the title Bug#10442 BUG: astype(str) on datetimelike index #10442 Sep 21, 2015
@jreback jreback added this to the 0.17.1 milestone Sep 21, 2015
@hamedhsn
Copy link
Author

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():
Copy link
Contributor

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

@jreback
Copy link
Contributor

jreback commented Oct 5, 2015

pls add a note in whatsnew for 0.17.1 (enhancements). squash as well.

@jreback
Copy link
Contributor

jreback commented Oct 5, 2015

going to need tests for Series.astype(str) as well (put near tests in test_series or tseries/tests/test_timeseries.py wherever the existing tests for .astype are), add the issue as a comment in the tests.

@hamedhsn
Copy link
Author

hamedhsn commented Oct 5, 2015

@jreback
there is no v0.17.1.txt for 0.17.1 in whatsnew folder.

@jreback
Copy link
Contributor

jreback commented Oct 5, 2015

rebase on master
i put it their 2 days ago

tm.assert_index_equal(result, expected)

# test astype string with freqH and timezone
import pytz
Copy link
Contributor

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

@jreback
Copy link
Contributor

jreback commented Oct 9, 2015

@hamedhsn can you update

@hamedhsn
Copy link
Author

hamedhsn commented Oct 9, 2015

@jreback sorry, what do I need to update?

@jreback
Copy link
Contributor

jreback commented Oct 9, 2015

comments above, needs a rebase as well

@hamedhsn
Copy link
Author

hamedhsn commented Oct 9, 2015

@jreback did that.
Thanks

@@ -17,6 +17,7 @@ Highlights include:

Enhancements
~~~~~~~~~~~~
- adding conversion of ``DatetimeIndex`` values to strings using ``astype(str)`` (:issue:`10442`)
Copy link
Contributor

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)

@jreback
Copy link
Contributor

jreback commented Oct 11, 2015

couple of comments. pls squash as well

@hamedhsn
Copy link
Author

@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
Copy link
Contributor

jreback commented Oct 11, 2015

@hamedhsn
Copy link
Author

@jreback squashed all the commits into one.

@jreback
Copy link
Contributor

jreback commented Oct 12, 2015

you need to make sure you force push too.

git checkout thisbranch
git rebase -i master

squash (put a s by each commit)

git push yourremote thisbranch -f

@hamedhsn
Copy link
Author

@jreback did that. thanks

result = ['2012-01-01', '2012-01-02', '2012-01-03', '2012-01-04']
assert(type(result[0]) == np.str)


Copy link
Contributor

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

@hamedhsn
Copy link
Author

@jreback should be fine now.

@jreback
Copy link
Contributor

jreback commented Oct 13, 2015

ok @hamedhsn ping on green.

@hamedhsn
Copy link
Author

@jreback strange, not sure why test is failed. both sides are object type but it raises this error..
AssertionError: [index] Expected object to be of type class 'pandas.core.index.Index',
found class 'pandas.core.series.Series' instead

I tried the test with both side same nothing to do with astype but still get the same error
result = pd.Series(pd.date_range('2012-01-01', periods=3))
expected = result
tm.assert_index_equal(result, expected)

# 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)
Copy link
Contributor

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
@hamedhsn
Copy link
Author

@jreback it is fine now.

jreback added a commit that referenced this pull request Oct 16, 2015
BUG: astype(str) on datetimelike index #10442
@jreback jreback merged commit 472e6e0 into pandas-dev:master Oct 16, 2015
@jreback
Copy link
Contributor

jreback commented Oct 16, 2015

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Convert datetimelike index to strings with astype(str)
4 participants