Skip to content

Commit 3e1cc56

Browse files
DEPR: Index.to_native_types (#36418)
1 parent 353ce7e commit 3e1cc56

File tree

6 files changed

+41
-26
lines changed

6 files changed

+41
-26
lines changed

Diff for: doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ Deprecations
212212
- Deprecated parameter ``dtype`` in :~meth:`Index.copy` on method all index classes. Use the :meth:`Index.astype` method instead for changing dtype(:issue:`35853`)
213213
- Date parser functions :func:`~pandas.io.date_converters.parse_date_time`, :func:`~pandas.io.date_converters.parse_date_fields`, :func:`~pandas.io.date_converters.parse_all_fields` and :func:`~pandas.io.date_converters.generic_parser` from ``pandas.io.date_converters`` are deprecated and will be removed in a future version; use :func:`to_datetime` instead (:issue:`35741`)
214214
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
215+
- The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)
215216

216217
.. ---------------------------------------------------------------------------
217218

Diff for: pandas/core/indexes/base.py

+8
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ def to_native_types(self, slicer=None, **kwargs):
10211021
"""
10221022
Format specified values of `self` and return them.
10231023
1024+
.. deprecated:: 1.2.0
1025+
10241026
Parameters
10251027
----------
10261028
slicer : int, array-like
@@ -1042,6 +1044,12 @@ def to_native_types(self, slicer=None, **kwargs):
10421044
numpy.ndarray
10431045
Formatted values.
10441046
"""
1047+
warnings.warn(
1048+
"The 'to_native_types' method is deprecated and will be removed in "
1049+
"a future version. Use 'astype(str)' instead.",
1050+
FutureWarning,
1051+
stacklevel=2,
1052+
)
10451053
values = self
10461054
if slicer is not None:
10471055
values = values[slicer]

Diff for: pandas/io/formats/csvs.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _refine_cols(self, cols: Optional[Sequence[Label]]) -> Sequence[Label]:
154154

155155
if cols is not None:
156156
if isinstance(cols, ABCIndexClass):
157-
cols = cols.to_native_types(**self._number_format)
157+
cols = cols._format_native_types(**self._number_format)
158158
else:
159159
cols = list(cols)
160160
self.obj = self.obj.loc[:, cols]
@@ -163,7 +163,7 @@ def _refine_cols(self, cols: Optional[Sequence[Label]]) -> Sequence[Label]:
163163
# and make sure sure cols is just a list of labels
164164
cols = self.obj.columns
165165
if isinstance(cols, ABCIndexClass):
166-
return cols.to_native_types(**self._number_format)
166+
return cols._format_native_types(**self._number_format)
167167
else:
168168
assert isinstance(cols, Sequence)
169169
return list(cols)
@@ -341,5 +341,5 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
341341
res = df._mgr.to_native_types(**self._number_format)
342342
data = [res.iget_values(i) for i in range(len(res.items))]
343343

344-
ix = self.data_index.to_native_types(slicer=slicer, **self._number_format)
344+
ix = self.data_index[slicer]._format_native_types(**self._number_format)
345345
libwriters.write_csv_rows(data, ix, self.nlevels, self.cols, self.writer)

Diff for: pandas/tests/indexes/datetimes/test_formats.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,53 @@
1010
import pandas._testing as tm
1111

1212

13-
def test_to_native_types():
13+
def test_to_native_types_method_deprecated():
1414
index = pd.date_range(freq="1D", periods=3, start="2017-01-01")
15-
16-
# First, with no arguments.
1715
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)
1816

19-
result = index.to_native_types()
20-
tm.assert_numpy_array_equal(result, expected)
17+
with tm.assert_produces_warning(FutureWarning):
18+
result = index.to_native_types()
2119

22-
# No NaN values, so na_rep has no effect
23-
result = index.to_native_types(na_rep="pandas")
2420
tm.assert_numpy_array_equal(result, expected)
2521

2622
# Make sure slicing works
2723
expected = np.array(["2017-01-01", "2017-01-03"], dtype=object)
2824

29-
result = index.to_native_types([0, 2])
25+
with tm.assert_produces_warning(FutureWarning):
26+
result = index.to_native_types([0, 2])
27+
28+
tm.assert_numpy_array_equal(result, expected)
29+
30+
31+
def test_to_native_types():
32+
index = pd.date_range(freq="1D", periods=3, start="2017-01-01")
33+
34+
# First, with no arguments.
35+
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)
36+
37+
result = index._format_native_types()
38+
tm.assert_numpy_array_equal(result, expected)
39+
40+
# No NaN values, so na_rep has no effect
41+
result = index._format_native_types(na_rep="pandas")
3042
tm.assert_numpy_array_equal(result, expected)
3143

3244
# Make sure date formatting works
3345
expected = np.array(["01-2017-01", "01-2017-02", "01-2017-03"], dtype=object)
3446

35-
result = index.to_native_types(date_format="%m-%Y-%d")
47+
result = index._format_native_types(date_format="%m-%Y-%d")
3648
tm.assert_numpy_array_equal(result, expected)
3749

3850
# NULL object handling should work
3951
index = DatetimeIndex(["2017-01-01", pd.NaT, "2017-01-03"])
4052
expected = np.array(["2017-01-01", "NaT", "2017-01-03"], dtype=object)
4153

42-
result = index.to_native_types()
54+
result = index._format_native_types()
4355
tm.assert_numpy_array_equal(result, expected)
4456

4557
expected = np.array(["2017-01-01", "pandas", "2017-01-03"], dtype=object)
4658

47-
result = index.to_native_types(na_rep="pandas")
59+
result = index._format_native_types(na_rep="pandas")
4860
tm.assert_numpy_array_equal(result, expected)
4961

5062

Diff for: pandas/tests/indexes/interval/test_formats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ def test_repr_missing(self, constructor, expected):
7373
def test_to_native_types(self, tuples, closed, expected_data):
7474
# GH 28210
7575
index = IntervalIndex.from_tuples(tuples, closed=closed)
76-
result = index.to_native_types()
76+
result = index._format_native_types()
7777
expected = np.array(expected_data)
7878
tm.assert_numpy_array_equal(result, expected)

Diff for: pandas/tests/indexes/period/test_formats.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,29 @@ def test_to_native_types():
1212
# First, with no arguments.
1313
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype="=U10")
1414

15-
result = index.to_native_types()
15+
result = index._format_native_types()
1616
tm.assert_numpy_array_equal(result, expected)
1717

1818
# No NaN values, so na_rep has no effect
19-
result = index.to_native_types(na_rep="pandas")
20-
tm.assert_numpy_array_equal(result, expected)
21-
22-
# Make sure slicing works
23-
expected = np.array(["2017-01-01", "2017-01-03"], dtype="=U10")
24-
25-
result = index.to_native_types([0, 2])
19+
result = index._format_native_types(na_rep="pandas")
2620
tm.assert_numpy_array_equal(result, expected)
2721

2822
# Make sure date formatting works
2923
expected = np.array(["01-2017-01", "01-2017-02", "01-2017-03"], dtype="=U10")
3024

31-
result = index.to_native_types(date_format="%m-%Y-%d")
25+
result = index._format_native_types(date_format="%m-%Y-%d")
3226
tm.assert_numpy_array_equal(result, expected)
3327

3428
# NULL object handling should work
3529
index = PeriodIndex(["2017-01-01", pd.NaT, "2017-01-03"], freq="D")
3630
expected = np.array(["2017-01-01", "NaT", "2017-01-03"], dtype=object)
3731

38-
result = index.to_native_types()
32+
result = index._format_native_types()
3933
tm.assert_numpy_array_equal(result, expected)
4034

4135
expected = np.array(["2017-01-01", "pandas", "2017-01-03"], dtype=object)
4236

43-
result = index.to_native_types(na_rep="pandas")
37+
result = index._format_native_types(na_rep="pandas")
4438
tm.assert_numpy_array_equal(result, expected)
4539

4640

0 commit comments

Comments
 (0)