-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
BUG: Fix Series doesn't work in pd.astype(). Now treat Series as dict. #16725
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 1 commit
2351a2e
37fc561
930c479
342871c
ce7e131
0280e5c
7d88920
08eabd9
fbd0755
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 |
---|---|---|
|
@@ -487,6 +487,54 @@ def test_astype_dict(self): | |
assert_frame_equal(df, equiv) | ||
assert_frame_equal(df, original) | ||
|
||
def test_astype_Series(self): | ||
# GH16717 | ||
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 would instead modify the previous the test and use parametrize 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. then just add the issue number as an additional comment 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. Done as your suggestion. |
||
a = Series(date_range('2010-01-04', periods=5)) | ||
b = Series(range(5)) | ||
c = Series([0.0, 0.2, 0.4, 0.6, 0.8]) | ||
d = Series(['1.0', '2', '3.14', '4', '5.4']) | ||
df = DataFrame({'a': a, 'b': b, 'c': c, 'd': d}) | ||
original = df.copy(deep=True) | ||
|
||
# change type of a subset of columns | ||
result = df.astype(Series({'b': 'str', 'd': 'float32'})) | ||
expected = DataFrame({ | ||
'a': a, | ||
'b': Series(['0', '1', '2', '3', '4']), | ||
'c': c, | ||
'd': Series([1.0, 2.0, 3.14, 4.0, 5.4], dtype='float32')}) | ||
assert_frame_equal(result, expected) | ||
assert_frame_equal(df, original) | ||
|
||
result = df.astype(Series( | ||
{'b': np.float32, | ||
'c': 'float32', | ||
'd': np.float64})) | ||
expected = DataFrame({ | ||
'a': a, | ||
'b': Series([0.0, 1.0, 2.0, 3.0, 4.0], dtype='float32'), | ||
'c': Series([0.0, 0.2, 0.4, 0.6, 0.8], dtype='float32'), | ||
'd': Series([1.0, 2.0, 3.14, 4.0, 5.4], dtype='float64')}) | ||
assert_frame_equal(result, expected) | ||
assert_frame_equal(df, original) | ||
|
||
# change all columns | ||
result = df.astype(Series({'a': str, 'b': str, 'c': str, 'd': str})) | ||
assert_frame_equal(result, df.astype(str)) | ||
assert_frame_equal(df, original) | ||
|
||
# error should be raised when using something other than column labels | ||
# in the keys of the dtype dict | ||
pytest.raises(KeyError, df.astype, Series({'b': str, 2: str})) | ||
pytest.raises(KeyError, df.astype, Series({'e': str})) | ||
assert_frame_equal(df, original) | ||
|
||
# if the dtypes provided are the same as the original dtypes, the | ||
# resulting DataFrame should be the same as the original DataFrame | ||
equiv = df.astype(Series({col: df[col].dtype for col in df.columns})) | ||
assert_frame_equal(df, equiv) | ||
assert_frame_equal(df, original) | ||
|
||
def test_astype_duplicate_col(self): | ||
a1 = Series([1, 2, 3, 4, 5], name='a') | ||
b = Series([0.1, 0.2, 0.4, 0.6, 0.8], name='b') | ||
|
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.
Bug in :func:`DataFarme.astype` where a passed Series as a dtype mapping would be ignored
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 the issue number as well
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.
Added.