|
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 |
|
7 |
| -from pandas import (date_range, period_range, |
8 |
| - Series, Index, DatetimeIndex, |
9 |
| - TimedeltaIndex, PeriodIndex) |
| 7 | +from pandas import (DatetimeIndex, Float64Index, Index, Int64Index, |
| 8 | + NaT, Period, PeriodIndex, Series, Timedelta, |
| 9 | + TimedeltaIndex, date_range, period_range, |
| 10 | + timedelta_range) |
10 | 11 |
|
11 | 12 | import pandas.util.testing as tm
|
12 | 13 |
|
@@ -337,6 +338,117 @@ def test_construction_dti_with_mixed_timezones(self):
|
337 | 338 | Timestamp('2011-01-02 10:00', tz='US/Eastern')],
|
338 | 339 | tz='US/Eastern', name='idx')
|
339 | 340 |
|
| 341 | + def test_astype(self): |
| 342 | + # GH 13149, GH 13209 |
| 343 | + idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) |
| 344 | + |
| 345 | + result = idx.astype(object) |
| 346 | + expected = Index([Timestamp('2016-05-16')] + [NaT] * 3, dtype=object) |
| 347 | + tm.assert_index_equal(result, expected) |
| 348 | + |
| 349 | + result = idx.astype(int) |
| 350 | + expected = Int64Index([1463356800000000000] + |
| 351 | + [-9223372036854775808] * 3, dtype=np.int64) |
| 352 | + tm.assert_index_equal(result, expected) |
| 353 | + |
| 354 | + rng = date_range('1/1/2000', periods=10) |
| 355 | + result = rng.astype('i8') |
| 356 | + self.assert_numpy_array_equal(result, rng.asi8) |
| 357 | + |
| 358 | + def test_astype_with_tz(self): |
| 359 | + |
| 360 | + # with tz |
| 361 | + rng = date_range('1/1/2000', periods=10, tz='US/Eastern') |
| 362 | + result = rng.astype('datetime64[ns]') |
| 363 | + expected = (date_range('1/1/2000', periods=10, |
| 364 | + tz='US/Eastern') |
| 365 | + .tz_convert('UTC').tz_localize(None)) |
| 366 | + tm.assert_index_equal(result, expected) |
| 367 | + |
| 368 | + # BUG#10442 : testing astype(str) is correct for Series/DatetimeIndex |
| 369 | + result = pd.Series(pd.date_range('2012-01-01', periods=3)).astype(str) |
| 370 | + expected = pd.Series( |
| 371 | + ['2012-01-01', '2012-01-02', '2012-01-03'], dtype=object) |
| 372 | + tm.assert_series_equal(result, expected) |
| 373 | + |
| 374 | + result = Series(pd.date_range('2012-01-01', periods=3, |
| 375 | + tz='US/Eastern')).astype(str) |
| 376 | + expected = Series(['2012-01-01 00:00:00-05:00', |
| 377 | + '2012-01-02 00:00:00-05:00', |
| 378 | + '2012-01-03 00:00:00-05:00'], |
| 379 | + dtype=object) |
| 380 | + tm.assert_series_equal(result, expected) |
| 381 | + |
| 382 | + def test_astype_str_compat(self): |
| 383 | + # GH 13149, GH 13209 |
| 384 | + # verify that we are returing NaT as a string (and not unicode) |
| 385 | + |
| 386 | + idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) |
| 387 | + result = idx.astype(str) |
| 388 | + expected = Index(['2016-05-16', 'NaT', 'NaT', 'NaT'], dtype=object) |
| 389 | + tm.assert_index_equal(result, expected) |
| 390 | + |
| 391 | + def test_astype_str(self): |
| 392 | + # test astype string - #10442 |
| 393 | + result = date_range('2012-01-01', periods=4, |
| 394 | + name='test_name').astype(str) |
| 395 | + expected = Index(['2012-01-01', '2012-01-02', '2012-01-03', |
| 396 | + '2012-01-04'], name='test_name', dtype=object) |
| 397 | + tm.assert_index_equal(result, expected) |
| 398 | + |
| 399 | + # test astype string with tz and name |
| 400 | + result = date_range('2012-01-01', periods=3, name='test_name', |
| 401 | + tz='US/Eastern').astype(str) |
| 402 | + expected = Index(['2012-01-01 00:00:00-05:00', |
| 403 | + '2012-01-02 00:00:00-05:00', |
| 404 | + '2012-01-03 00:00:00-05:00'], |
| 405 | + name='test_name', dtype=object) |
| 406 | + tm.assert_index_equal(result, expected) |
| 407 | + |
| 408 | + # test astype string with freqH and name |
| 409 | + result = date_range('1/1/2011', periods=3, freq='H', |
| 410 | + name='test_name').astype(str) |
| 411 | + expected = Index(['2011-01-01 00:00:00', '2011-01-01 01:00:00', |
| 412 | + '2011-01-01 02:00:00'], |
| 413 | + name='test_name', dtype=object) |
| 414 | + tm.assert_index_equal(result, expected) |
| 415 | + |
| 416 | + # test astype string with freqH and timezone |
| 417 | + result = date_range('3/6/2012 00:00', periods=2, freq='H', |
| 418 | + tz='Europe/London', name='test_name').astype(str) |
| 419 | + expected = Index(['2012-03-06 00:00:00+00:00', |
| 420 | + '2012-03-06 01:00:00+00:00'], |
| 421 | + dtype=object, name='test_name') |
| 422 | + tm.assert_index_equal(result, expected) |
| 423 | + |
| 424 | + def test_astype_datetime64(self): |
| 425 | + # GH 13149, GH 13209 |
| 426 | + idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) |
| 427 | + |
| 428 | + result = idx.astype('datetime64[ns]') |
| 429 | + tm.assert_index_equal(result, idx) |
| 430 | + self.assertFalse(result is idx) |
| 431 | + |
| 432 | + result = idx.astype('datetime64[ns]', copy=False) |
| 433 | + tm.assert_index_equal(result, idx) |
| 434 | + self.assertTrue(result is idx) |
| 435 | + |
| 436 | + idx_tz = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN], tz='EST') |
| 437 | + result = idx_tz.astype('datetime64[ns]') |
| 438 | + expected = DatetimeIndex(['2016-05-16 05:00:00', 'NaT', 'NaT', 'NaT'], |
| 439 | + dtype='datetime64[ns]') |
| 440 | + tm.assert_index_equal(result, expected) |
| 441 | + |
| 442 | + def test_astype_raises(self): |
| 443 | + # GH 13149, GH 13209 |
| 444 | + idx = DatetimeIndex(['2016-05-16', 'NaT', NaT, np.NaN]) |
| 445 | + |
| 446 | + self.assertRaises(ValueError, idx.astype, float) |
| 447 | + self.assertRaises(ValueError, idx.astype, 'timedelta64') |
| 448 | + self.assertRaises(ValueError, idx.astype, 'timedelta64[ns]') |
| 449 | + self.assertRaises(ValueError, idx.astype, 'datetime64') |
| 450 | + self.assertRaises(ValueError, idx.astype, 'datetime64[D]') |
| 451 | + |
340 | 452 | def test_get_loc(self):
|
341 | 453 | idx = pd.date_range('2000-01-01', periods=3)
|
342 | 454 |
|
@@ -585,6 +697,42 @@ def setUp(self):
|
585 | 697 | def create_index(self):
|
586 | 698 | return period_range('20130101', periods=5, freq='D')
|
587 | 699 |
|
| 700 | + def test_astype(self): |
| 701 | + # GH 13149, GH 13209 |
| 702 | + idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') |
| 703 | + |
| 704 | + result = idx.astype(object) |
| 705 | + expected = Index([Period('2016-05-16', freq='D')] + |
| 706 | + [Period(NaT, freq='D')] * 3, dtype='object') |
| 707 | + # Hack because of lack of support for Period null checking (GH12759) |
| 708 | + tm.assert_index_equal(result[:1], expected[:1]) |
| 709 | + result_arr = np.asarray([p.ordinal for p in result], dtype=np.int64) |
| 710 | + expected_arr = np.asarray([p.ordinal for p in expected], |
| 711 | + dtype=np.int64) |
| 712 | + tm.assert_numpy_array_equal(result_arr, expected_arr) |
| 713 | + # TODO: When GH12759 is resolved, change the above hack to: |
| 714 | + # tm.assert_index_equal(result, expected) # now, it raises. |
| 715 | + |
| 716 | + result = idx.astype(int) |
| 717 | + expected = Int64Index([16937] + [-9223372036854775808] * 3, |
| 718 | + dtype=np.int64) |
| 719 | + tm.assert_index_equal(result, expected) |
| 720 | + |
| 721 | + idx = period_range('1990', '2009', freq='A') |
| 722 | + result = idx.astype('i8') |
| 723 | + self.assert_numpy_array_equal(result, idx.values) |
| 724 | + |
| 725 | + def test_astype_raises(self): |
| 726 | + # GH 13149, GH 13209 |
| 727 | + idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') |
| 728 | + |
| 729 | + self.assertRaises(ValueError, idx.astype, str) |
| 730 | + self.assertRaises(ValueError, idx.astype, float) |
| 731 | + self.assertRaises(ValueError, idx.astype, 'timedelta64') |
| 732 | + self.assertRaises(ValueError, idx.astype, 'timedelta64[ns]') |
| 733 | + self.assertRaises(ValueError, idx.astype, 'datetime64') |
| 734 | + self.assertRaises(ValueError, idx.astype, 'datetime64[ns]') |
| 735 | + |
588 | 736 | def test_shift(self):
|
589 | 737 |
|
590 | 738 | # test shift for PeriodIndex
|
@@ -726,6 +874,50 @@ def test_shift(self):
|
726 | 874 | '10 days 01:00:03'], freq='D')
|
727 | 875 | self.assert_index_equal(result, expected)
|
728 | 876 |
|
| 877 | + def test_astype(self): |
| 878 | + # GH 13149, GH 13209 |
| 879 | + idx = TimedeltaIndex([1e14, 'NaT', pd.NaT, np.NaN]) |
| 880 | + |
| 881 | + result = idx.astype(object) |
| 882 | + expected = Index([Timedelta('1 days 03:46:40')] + [pd.NaT] * 3, |
| 883 | + dtype=object) |
| 884 | + tm.assert_index_equal(result, expected) |
| 885 | + |
| 886 | + result = idx.astype(int) |
| 887 | + expected = Int64Index([100000000000000] + [-9223372036854775808] * 3, |
| 888 | + dtype=np.int64) |
| 889 | + tm.assert_index_equal(result, expected) |
| 890 | + |
| 891 | + rng = timedelta_range('1 days', periods=10) |
| 892 | + |
| 893 | + result = rng.astype('i8') |
| 894 | + self.assert_numpy_array_equal(result, rng.asi8) |
| 895 | + |
| 896 | + def test_astype_timedelta64(self): |
| 897 | + # GH 13149, GH 13209 |
| 898 | + idx = TimedeltaIndex([1e14, 'NaT', pd.NaT, np.NaN]) |
| 899 | + |
| 900 | + result = idx.astype('timedelta64') |
| 901 | + expected = Float64Index([1e+14] + [np.NaN] * 3, dtype='float64') |
| 902 | + tm.assert_index_equal(result, expected) |
| 903 | + |
| 904 | + result = idx.astype('timedelta64[ns]') |
| 905 | + tm.assert_index_equal(result, idx) |
| 906 | + self.assertFalse(result is idx) |
| 907 | + |
| 908 | + result = idx.astype('timedelta64[ns]', copy=False) |
| 909 | + tm.assert_index_equal(result, idx) |
| 910 | + self.assertTrue(result is idx) |
| 911 | + |
| 912 | + def test_astype_raises(self): |
| 913 | + # GH 13149, GH 13209 |
| 914 | + idx = TimedeltaIndex([1e14, 'NaT', pd.NaT, np.NaN]) |
| 915 | + |
| 916 | + self.assertRaises(ValueError, idx.astype, float) |
| 917 | + self.assertRaises(ValueError, idx.astype, str) |
| 918 | + self.assertRaises(ValueError, idx.astype, 'datetime64') |
| 919 | + self.assertRaises(ValueError, idx.astype, 'datetime64[ns]') |
| 920 | + |
729 | 921 | def test_get_loc(self):
|
730 | 922 | idx = pd.to_timedelta(['0 days', '1 days', '2 days'])
|
731 | 923 |
|
|
0 commit comments