Skip to content

Commit

Permalink
DEPR: Index.to_native_types (#36418)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored Sep 19, 2020
1 parent 353ce7e commit 3e1cc56
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ Deprecations
- Deprecated parameter ``dtype`` in :~meth:`Index.copy` on method all index classes. Use the :meth:`Index.astype` method instead for changing dtype(:issue:`35853`)
- 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`)
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
- The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)

.. ---------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ def to_native_types(self, slicer=None, **kwargs):
"""
Format specified values of `self` and return them.
.. deprecated:: 1.2.0
Parameters
----------
slicer : int, array-like
Expand All @@ -1042,6 +1044,12 @@ def to_native_types(self, slicer=None, **kwargs):
numpy.ndarray
Formatted values.
"""
warnings.warn(
"The 'to_native_types' method is deprecated and will be removed in "
"a future version. Use 'astype(str)' instead.",
FutureWarning,
stacklevel=2,
)
values = self
if slicer is not None:
values = values[slicer]
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _refine_cols(self, cols: Optional[Sequence[Label]]) -> Sequence[Label]:

if cols is not None:
if isinstance(cols, ABCIndexClass):
cols = cols.to_native_types(**self._number_format)
cols = cols._format_native_types(**self._number_format)
else:
cols = list(cols)
self.obj = self.obj.loc[:, cols]
Expand All @@ -163,7 +163,7 @@ def _refine_cols(self, cols: Optional[Sequence[Label]]) -> Sequence[Label]:
# and make sure sure cols is just a list of labels
cols = self.obj.columns
if isinstance(cols, ABCIndexClass):
return cols.to_native_types(**self._number_format)
return cols._format_native_types(**self._number_format)
else:
assert isinstance(cols, Sequence)
return list(cols)
Expand Down Expand Up @@ -341,5 +341,5 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
res = df._mgr.to_native_types(**self._number_format)
data = [res.iget_values(i) for i in range(len(res.items))]

ix = self.data_index.to_native_types(slicer=slicer, **self._number_format)
ix = self.data_index[slicer]._format_native_types(**self._number_format)
libwriters.write_csv_rows(data, ix, self.nlevels, self.cols, self.writer)
34 changes: 23 additions & 11 deletions pandas/tests/indexes/datetimes/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,53 @@
import pandas._testing as tm


def test_to_native_types():
def test_to_native_types_method_deprecated():
index = pd.date_range(freq="1D", periods=3, start="2017-01-01")

# First, with no arguments.
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)

result = index.to_native_types()
tm.assert_numpy_array_equal(result, expected)
with tm.assert_produces_warning(FutureWarning):
result = index.to_native_types()

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

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

result = index.to_native_types([0, 2])
with tm.assert_produces_warning(FutureWarning):
result = index.to_native_types([0, 2])

tm.assert_numpy_array_equal(result, expected)


def test_to_native_types():
index = pd.date_range(freq="1D", periods=3, start="2017-01-01")

# First, with no arguments.
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)

result = index._format_native_types()
tm.assert_numpy_array_equal(result, expected)

# No NaN values, so na_rep has no effect
result = index._format_native_types(na_rep="pandas")
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types(date_format="%m-%Y-%d")
result = index._format_native_types(date_format="%m-%Y-%d")
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types()
result = index._format_native_types()
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types(na_rep="pandas")
result = index._format_native_types(na_rep="pandas")
tm.assert_numpy_array_equal(result, expected)


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/interval/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ def test_repr_missing(self, constructor, expected):
def test_to_native_types(self, tuples, closed, expected_data):
# GH 28210
index = IntervalIndex.from_tuples(tuples, closed=closed)
result = index.to_native_types()
result = index._format_native_types()
expected = np.array(expected_data)
tm.assert_numpy_array_equal(result, expected)
16 changes: 5 additions & 11 deletions pandas/tests/indexes/period/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,29 @@ def test_to_native_types():
# First, with no arguments.
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype="=U10")

result = index.to_native_types()
result = index._format_native_types()
tm.assert_numpy_array_equal(result, expected)

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

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

result = index.to_native_types([0, 2])
result = index._format_native_types(na_rep="pandas")
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types(date_format="%m-%Y-%d")
result = index._format_native_types(date_format="%m-%Y-%d")
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types()
result = index._format_native_types()
tm.assert_numpy_array_equal(result, expected)

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

result = index.to_native_types(na_rep="pandas")
result = index._format_native_types(na_rep="pandas")
tm.assert_numpy_array_equal(result, expected)


Expand Down

0 comments on commit 3e1cc56

Please sign in to comment.