Skip to content

Commit 7165fdb

Browse files
committed
TST: Test to_native_types for Period/DatetimeIndex
[ci skip]
1 parent 2126601 commit 7165fdb

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from pandas import DatetimeIndex
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 = DatetimeIndex(freq='1D', periods=3, start='2017-01-01')
11+
12+
# First, with no arguments.
13+
expected = np.array(['2017-01-01', '2017-01-02',
14+
'2017-01-03'], dtype=object)
15+
16+
result = index.to_native_types()
17+
tm.assert_numpy_array_equal(result, expected)
18+
19+
# No NaN values, so na_rep has no effect
20+
result = index.to_native_types(na_rep='pandas')
21+
tm.assert_numpy_array_equal(result, expected)
22+
23+
# Make sure slicing works
24+
expected = np.array(['2017-01-01', '2017-01-03'], dtype=object)
25+
26+
result = index.to_native_types([0, 2])
27+
tm.assert_numpy_array_equal(result, expected)
28+
29+
# Make sure date formatting works
30+
expected = np.array(['01-2017-01', '01-2017-02',
31+
'01-2017-03'], dtype=object)
32+
33+
result = index.to_native_types(date_format='%m-%Y-%d')
34+
tm.assert_numpy_array_equal(result, expected)
35+
36+
# NULL object handling should work
37+
index = DatetimeIndex(['2017-01-01', pd.NaT, '2017-01-03'])
38+
expected = np.array(['2017-01-01', 'NaT', '2017-01-03'], dtype=object)
39+
40+
result = index.to_native_types()
41+
tm.assert_numpy_array_equal(result, expected)
42+
43+
expected = np.array(['2017-01-01', 'pandas',
44+
'2017-01-03'], dtype=object)
45+
46+
result = index.to_native_types(na_rep='pandas')
47+
tm.assert_numpy_array_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)