Skip to content

Commit 06cf781

Browse files
committed
TST: Test to_native_types for Period/DatetimeIndex
1 parent 6bed25b commit 06cf781

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

pandas/indexes/base.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,26 @@ def _format_with_header(self, header, na_rep='NaN', **kwargs):
18201820
return header + result
18211821

18221822
def to_native_types(self, slicer=None, **kwargs):
1823-
""" slice and dice then format """
1823+
"""
1824+
Format specified values of `self` and return them.
1825+
1826+
Parameters
1827+
----------
1828+
slicer : int, array-like
1829+
An indexer into `self` that specifies which values
1830+
are used in the formatting process.
1831+
kwargs : dict
1832+
Options for specifying how the values should be formatted.
1833+
These options include the following:
1834+
1835+
1) na_rep : str
1836+
The value that serves as a placeholder for NULL values
1837+
2) quoting : bool or None
1838+
Whether or not there are quoted values in `self`
1839+
3) date_format : str
1840+
The format used to represent date-like values
1841+
"""
1842+
18241843
values = self
18251844
if slicer is not None:
18261845
values = values[slicer]

pandas/tests/indexes/datetimes/test_datetime.py

+40
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,43 @@ def test_slice_bounds_empty(self):
773773
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
774774
exp = Timestamp('2015-01-02 00:00:00')
775775
self.assertEqual(left, exp)
776+
777+
def test_to_native_types(self):
778+
index = DatetimeIndex(freq='1D', periods=3, start='2017-01-01')
779+
780+
# First, with no arguments.
781+
expected = np.array(['2017-01-01', '2017-01-02',
782+
'2017-01-03'], dtype=object)
783+
784+
result = index.to_native_types()
785+
tm.assert_numpy_array_equal(result, expected)
786+
787+
# No NaN values, so na_rep has no effect
788+
result = index.to_native_types(na_rep='pandas')
789+
tm.assert_numpy_array_equal(result, expected)
790+
791+
# Make sure slicing works
792+
expected = np.array(['2017-01-01', '2017-01-03'], dtype=object)
793+
794+
result = index.to_native_types([0, 2])
795+
tm.assert_numpy_array_equal(result, expected)
796+
797+
# Make sure date formatting works
798+
expected = np.array(['01-2017-01', '01-2017-02',
799+
'01-2017-03'], dtype=object)
800+
801+
result = index.to_native_types(date_format='%m-%Y-%d')
802+
tm.assert_numpy_array_equal(result, expected)
803+
804+
# NULL object handling should work
805+
index = DatetimeIndex(['2017-01-01', pd.NaT, '2017-01-03'])
806+
expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object)
807+
808+
result = index.to_native_types()
809+
tm.assert_numpy_array_equal(result, expected)
810+
811+
expected = np.array(['2017-01-01', 'pandas',
812+
'2017-01-03'], dtype=object)
813+
814+
result = index.to_native_types(na_rep='pandas')
815+
tm.assert_numpy_array_equal(result, expected)

pandas/tests/indexes/period/test_period.py

+41
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,44 @@ def test_map(self):
774774
result = index.map(lambda x: x.ordinal)
775775
exp = Index([x.ordinal for x in index])
776776
tm.assert_index_equal(result, exp)
777+
778+
def test_to_native_types(self):
779+
index = PeriodIndex(['2017-01-01', '2017-01-02',
780+
'2017-01-03'], freq='D')
781+
782+
# First, with no arguments.
783+
expected = np.array(['2017-01-01', '2017-01-02',
784+
'2017-01-03'], dtype='<U10')
785+
786+
result = index.to_native_types()
787+
tm.assert_numpy_array_equal(result, expected)
788+
789+
# No NaN values, so na_rep has no effect
790+
result = index.to_native_types(na_rep='pandas')
791+
tm.assert_numpy_array_equal(result, expected)
792+
793+
# Make sure slicing works
794+
expected = np.array(['2017-01-01', '2017-01-03'], dtype='<U10')
795+
796+
result = index.to_native_types([0, 2])
797+
tm.assert_numpy_array_equal(result, expected)
798+
799+
# Make sure date formatting works
800+
expected = np.array(['01-2017-01', '01-2017-02',
801+
'01-2017-03'], dtype='<U10')
802+
803+
result = index.to_native_types(date_format='%m-%Y-%d')
804+
tm.assert_numpy_array_equal(result, expected)
805+
806+
# NULL object handling should work
807+
index = PeriodIndex(['2017-01-01', pd.NaT, '2017-01-03'], freq='D')
808+
expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object)
809+
810+
result = index.to_native_types()
811+
tm.assert_numpy_array_equal(result, expected)
812+
813+
expected = np.array(['2017-01-01', 'pandas',
814+
'2017-01-03'], dtype=object)
815+
816+
result = index.to_native_types(na_rep='pandas')
817+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)