Skip to content

DEPR: move NumericIndex._format_native_types to Index #50931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,11 +1291,30 @@ def _format_with_header(self, header: list[str_t], na_rep: str_t) -> list[str_t]
return header + result

def _format_native_types(
self, *, na_rep: str_t = "", quoting=None, **kwargs
self,
*,
na_rep: str_t = "",
decimal: str_t = ".",
float_format=None,
date_format=None,
quoting=None,
) -> npt.NDArray[np.object_]:
"""
Actually format specific types of the index.
"""
from pandas.io.formats.format import FloatArrayFormatter

if is_float_dtype(self.dtype) and not is_extension_array_dtype(self.dtype):
formatter = FloatArrayFormatter(
self._values,
na_rep=na_rep,
float_format=float_format,
decimal=decimal,
quoting=quoting,
fixed_width=False,
)
return formatter.get_result_as_array()

mask = isna(self)
if not self.is_object() and not quoting:
values = np.asarray(self).astype(str)
Expand Down
35 changes: 1 addition & 34 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import numpy as np

from pandas._libs import index as libindex
from pandas._typing import (
Dtype,
npt,
)
from pandas._typing import Dtype
from pandas.util._decorators import (
cache_readonly,
doc,
Expand Down Expand Up @@ -251,33 +248,3 @@ def _assert_safe_casting(cls, data: np.ndarray, subarr: np.ndarray) -> None:
if is_integer_dtype(subarr.dtype):
if not np.array_equal(data, subarr):
raise TypeError("Unsafe NumPy casting, you must explicitly cast")

def _format_native_types(
self,
*,
na_rep: str = "",
float_format=None,
decimal: str = ".",
quoting=None,
**kwargs,
) -> npt.NDArray[np.object_]:
from pandas.io.formats.format import FloatArrayFormatter

if is_float_dtype(self.dtype):
formatter = FloatArrayFormatter(
self._values,
na_rep=na_rep,
float_format=float_format,
decimal=decimal,
quoting=quoting,
fixed_width=False,
)
return formatter.get_result_as_array()

return super()._format_native_types(
na_rep=na_rep,
float_format=float_format,
decimal=decimal,
quoting=quoting,
**kwargs,
)