|
| 1 | +from pandas import PeriodIndex |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import pandas.util.testing as tm |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | + |
| 9 | +def test_to_native_types(): |
| 10 | + index = PeriodIndex(['2017-01-01', '2017-01-02', |
| 11 | + '2017-01-03'], freq='D') |
| 12 | + |
| 13 | + # First, with no arguments. |
| 14 | + expected = np.array(['2017-01-01', '2017-01-02', |
| 15 | + '2017-01-03'], dtype='<U10') |
| 16 | + |
| 17 | + result = index.to_native_types() |
| 18 | + tm.assert_numpy_array_equal(result, expected) |
| 19 | + |
| 20 | + # No NaN values, so na_rep has no effect |
| 21 | + result = index.to_native_types(na_rep='pandas') |
| 22 | + tm.assert_numpy_array_equal(result, expected) |
| 23 | + |
| 24 | + # Make sure slicing works |
| 25 | + expected = np.array(['2017-01-01', '2017-01-03'], dtype='<U10') |
| 26 | + |
| 27 | + result = index.to_native_types([0, 2]) |
| 28 | + tm.assert_numpy_array_equal(result, expected) |
| 29 | + |
| 30 | + # Make sure date formatting works |
| 31 | + expected = np.array(['01-2017-01', '01-2017-02', |
| 32 | + '01-2017-03'], dtype='<U10') |
| 33 | + |
| 34 | + result = index.to_native_types(date_format='%m-%Y-%d') |
| 35 | + tm.assert_numpy_array_equal(result, expected) |
| 36 | + |
| 37 | + # NULL object handling should work |
| 38 | + index = PeriodIndex(['2017-01-01', pd.NaT, '2017-01-03'], freq='D') |
| 39 | + expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object) |
| 40 | + |
| 41 | + result = index.to_native_types() |
| 42 | + tm.assert_numpy_array_equal(result, expected) |
| 43 | + |
| 44 | + expected = np.array(['2017-01-01', 'pandas', |
| 45 | + '2017-01-03'], dtype=object) |
| 46 | + |
| 47 | + result = index.to_native_types(na_rep='pandas') |
| 48 | + tm.assert_numpy_array_equal(result, expected) |
0 commit comments