Skip to content

Commit 20cdf1e

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

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-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

+39
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,42 @@ 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', '2017-01-03'])
784+
785+
result = index.to_native_types()
786+
tm.assert_numpy_array_equal(result, expected)
787+
788+
# No NaN values, so na_rep has no effect
789+
result = index.to_native_types(na_rep='pandas')
790+
tm.assert_numpy_array_equal(result, expected)
791+
792+
# Make sure slicing works
793+
expected = np.array(['2017-01-01', '2017-01-03'])
794+
795+
result = index.to_native_types([0, 2])
796+
tm.assert_numpy_array_equal(result, expected)
797+
798+
# Make sure date formatting works
799+
expected = np.array(['01-2017-01', '01-2017-02', '01-2017-03'])
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 = PeriodIndex(['2017-01-01', pd.NaT, '2017-01-03'], freq='D')
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)

0 commit comments

Comments
 (0)