|
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 | from pandas import (DataFrame, Series, date_range, Timedelta, Timestamp,
|
8 |
| - compat, option_context) |
| 8 | + compat, concat, option_context) |
9 | 9 | from pandas.compat import u
|
10 | 10 | from pandas.types.dtypes import DatetimeTZDtype
|
11 | 11 | from pandas.tests.frame.common import TestData
|
@@ -396,6 +396,69 @@ def test_astype_str(self):
|
396 | 396 | expected = DataFrame(['1.12345678901'])
|
397 | 397 | assert_frame_equal(result, expected)
|
398 | 398 |
|
| 399 | + def test_astype_dict(self): |
| 400 | + # GH7271 |
| 401 | + a = Series(date_range('2010-01-04', periods=5)) |
| 402 | + b = Series(range(5)) |
| 403 | + c = Series([0.0, 0.2, 0.4, 0.6, 0.8]) |
| 404 | + d = Series(['1.0', '2', '3.14', '4', '5.4']) |
| 405 | + df = DataFrame({'a': a, 'b': b, 'c': c, 'd': d}) |
| 406 | + original = df.copy(deep=True) |
| 407 | + |
| 408 | + # change type of a subset of columns |
| 409 | + result = df.astype({'b': 'str', 'd': 'float32'}) |
| 410 | + expected = DataFrame({ |
| 411 | + 'a': a, |
| 412 | + 'b': Series(['0', '1', '2', '3', '4']), |
| 413 | + 'c': c, |
| 414 | + 'd': Series([1.0, 2.0, 3.14, 4.0, 5.4], dtype='float32')}) |
| 415 | + assert_frame_equal(result, expected) |
| 416 | + assert_frame_equal(df, original) |
| 417 | + |
| 418 | + result = df.astype({'b': np.float32, 'c': 'float32', 'd': np.float64}) |
| 419 | + expected = DataFrame({ |
| 420 | + 'a': a, |
| 421 | + 'b': Series([0.0, 1.0, 2.0, 3.0, 4.0], dtype='float32'), |
| 422 | + 'c': Series([0.0, 0.2, 0.4, 0.6, 0.8], dtype='float32'), |
| 423 | + 'd': Series([1.0, 2.0, 3.14, 4.0, 5.4], dtype='float64')}) |
| 424 | + assert_frame_equal(result, expected) |
| 425 | + assert_frame_equal(df, original) |
| 426 | + |
| 427 | + # change all columns |
| 428 | + assert_frame_equal(df.astype({'a': str, 'b': str, 'c': str, 'd': str}), |
| 429 | + df.astype(str)) |
| 430 | + assert_frame_equal(df, original) |
| 431 | + |
| 432 | + # error should be raised when using something other than column labels |
| 433 | + # in the keys of the dtype dict |
| 434 | + self.assertRaises(KeyError, df.astype, {'b': str, 2: str}) |
| 435 | + self.assertRaises(KeyError, df.astype, {'e': str}) |
| 436 | + assert_frame_equal(df, original) |
| 437 | + |
| 438 | + # if the dtypes provided are the same as the original dtypes, the |
| 439 | + # resulting DataFrame should be the same as the original DataFrame |
| 440 | + equiv = df.astype({col: df[col].dtype for col in df.columns}) |
| 441 | + assert_frame_equal(df, equiv) |
| 442 | + assert_frame_equal(df, original) |
| 443 | + |
| 444 | + def test_astype_duplicate_col(self): |
| 445 | + a1 = Series([1, 2, 3, 4, 5], name='a') |
| 446 | + b = Series([0.1, 0.2, 0.4, 0.6, 0.8], name='b') |
| 447 | + a2 = Series([0, 1, 2, 3, 4], name='a') |
| 448 | + df = concat([a1, b, a2], axis=1) |
| 449 | + |
| 450 | + result = df.astype(str) |
| 451 | + a1_str = Series(['1', '2', '3', '4', '5'], dtype='str', name='a') |
| 452 | + b_str = Series(['0.1', '0.2', '0.4', '0.6', '0.8'], dtype=str, |
| 453 | + name='b') |
| 454 | + a2_str = Series(['0', '1', '2', '3', '4'], dtype='str', name='a') |
| 455 | + expected = concat([a1_str, b_str, a2_str], axis=1) |
| 456 | + assert_frame_equal(result, expected) |
| 457 | + |
| 458 | + result = df.astype({'a': 'str'}) |
| 459 | + expected = concat([a1_str, b, a2_str], axis=1) |
| 460 | + assert_frame_equal(result, expected) |
| 461 | + |
399 | 462 | def test_timedeltas(self):
|
400 | 463 | df = DataFrame(dict(A=Series(date_range('2012-1-1', periods=3,
|
401 | 464 | freq='D')),
|
|
0 commit comments