Skip to content

CLN: pandas\io\formats\format.py #27577

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
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
8 changes: 5 additions & 3 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ def _formatter_func(self):
def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
from pandas.io.formats.format import Timedelta64Formatter

return Timedelta64Formatter(
values=self, nat_rep=na_rep, justify="all"
).get_result()
return np.asarray(
Timedelta64Formatter(
values=self, nat_rep=na_rep, justify="all"
).get_result()
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use asarray or pass copy=False

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. was keeping the code as close to original as possible.


# -------------------------------------------------------------------
# Wrapping TimedeltaArray
Expand Down
41 changes: 21 additions & 20 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from dateutil.tz.tz import tzutc
from dateutil.zoneinfo import tzfile
import numpy as np
from numpy import float64, int32, ndarray

from pandas._config.config import get_option, set_option

Expand Down Expand Up @@ -1124,7 +1123,7 @@ def __init__(
self.fixed_width = fixed_width
self.leading_space = leading_space

def get_result(self) -> Union[ndarray, List[str]]:
def get_result(self) -> List[str]:
fmt_values = self._format_strings()
return _make_fixed_width(fmt_values, self.justify)

Expand Down Expand Up @@ -1260,7 +1259,7 @@ def formatter(value):

return formatter

def get_result_as_array(self) -> Union[ndarray, List[str]]:
def get_result_as_array(self) -> np.ndarray:
"""
Returns the float values converted into strings using
the parameters given at initialisation, as a numpy array
Expand Down Expand Up @@ -1300,9 +1299,10 @@ def format_values_with(float_format):

if self.fixed_width:
if is_complex:
return _trim_zeros_complex(values, na_rep)
result = _trim_zeros_complex(values, na_rep)
else:
return _trim_zeros_float(values, na_rep)
result = _trim_zeros_float(values, na_rep)
return np.asarray(result, dtype="object")

return values

Expand Down Expand Up @@ -1367,7 +1367,7 @@ def _format_strings(self) -> List[str]:
class Datetime64Formatter(GenericArrayFormatter):
def __init__(
self,
values: Union[ndarray, "Series", DatetimeIndex, DatetimeArray],
values: Union[np.ndarray, "Series", DatetimeIndex, DatetimeArray],
nat_rep: str = "NaT",
date_format: None = None,
**kwargs
Expand Down Expand Up @@ -1424,7 +1424,7 @@ def _format_strings(self) -> List[str]:

def format_percentiles(
percentiles: Union[
ndarray, List[Union[int, float]], List[float], List[Union[str, float]]
np.ndarray, List[Union[int, float]], List[float], List[Union[str, float]]
]
) -> List[str]:
"""
Expand Down Expand Up @@ -1492,7 +1492,9 @@ def format_percentiles(
return [i + "%" for i in out]


def _is_dates_only(values: Union[ndarray, DatetimeArray, Index, DatetimeIndex]) -> bool:
def _is_dates_only(
values: Union[np.ndarray, DatetimeArray, Index, DatetimeIndex]
) -> bool:
# return a boolean if we are only dates (and don't have a timezone)
assert values.ndim == 1

Expand Down Expand Up @@ -1556,7 +1558,7 @@ def _get_format_datetime64(


def _get_format_datetime64_from_values(
values: Union[ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str]
values: Union[np.ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str]
) -> Optional[str]:
""" given values and a date_format, return a string format """

Expand Down Expand Up @@ -1588,7 +1590,7 @@ def _format_strings(self) -> List[str]:
class Timedelta64Formatter(GenericArrayFormatter):
def __init__(
self,
values: Union[ndarray, TimedeltaIndex],
values: Union[np.ndarray, TimedeltaIndex],
nat_rep: str = "NaT",
box: bool = False,
**kwargs
Expand All @@ -1597,16 +1599,15 @@ def __init__(
self.nat_rep = nat_rep
self.box = box

def _format_strings(self) -> ndarray:
def _format_strings(self) -> List[str]:
formatter = self.formatter or _get_format_timedelta64(
self.values, nat_rep=self.nat_rep, box=self.box
)
fmt_values = np.array([formatter(x) for x in self.values])
return fmt_values
return [formatter(x) for x in self.values]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the most relevant change here.

return type now consistent with parent class and other formatters.

mypy should have picked this up. but since we don't have numpy stubs, all numpy types resolve to Any



def _get_format_timedelta64(
values: Union[ndarray, TimedeltaIndex, TimedeltaArray],
values: Union[np.ndarray, TimedeltaIndex, TimedeltaArray],
nat_rep: str = "NaT",
box: bool = False,
) -> Callable:
Expand Down Expand Up @@ -1651,11 +1652,11 @@ def _formatter(x):


def _make_fixed_width(
strings: Union[ndarray, List[str]],
strings: List[str],
justify: str = "right",
minimum: Optional[int] = None,
adj: Optional[TextAdjustment] = None,
) -> Union[ndarray, List[str]]:
) -> List[str]:

if len(strings) == 0 or justify == "all":
return strings
Expand Down Expand Up @@ -1683,7 +1684,7 @@ def just(x):
return result


def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str]:
def _trim_zeros_complex(str_complexes: np.ndarray, na_rep: str = "NaN") -> List[str]:
"""
Separates the real and imaginary parts from the complex number, and
executes the _trim_zeros_float method on each of those.
Expand All @@ -1702,7 +1703,7 @@ def separate_and_trim(str_complex, na_rep):


def _trim_zeros_float(
str_floats: Union[ndarray, List[str]], na_rep: str = "NaN"
str_floats: Union[np.ndarray, List[str]], na_rep: str = "NaN"
) -> List[str]:
"""
Trims zeros, leaving just one before the decimal points if need be.
Expand Down Expand Up @@ -1766,7 +1767,7 @@ def __init__(self, accuracy: Optional[int] = None, use_eng_prefix: bool = False)
self.accuracy = accuracy
self.use_eng_prefix = use_eng_prefix

def __call__(self, num: Union[float64, int, float]) -> str:
def __call__(self, num: Union[int, float]) -> str:
""" Formats a number in engineering notation, appending a letter
representing the power of 1000 of the original number. Some examples:

Expand Down Expand Up @@ -1846,7 +1847,7 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non
set_option("display.column_space", max(12, accuracy + 9))


def _binify(cols: List[int32], line_width: Union[int32, int]) -> List[int]:
def _binify(cols: List[np.int32], line_width: Union[np.int32, int]) -> List[int]:
adjoin_width = 1
bins = []
curr_width = 0
Expand Down