diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 1c2a8c4f0c10c..1fc1684335982 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -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() + ) # ------------------------------------------------------------------- # Wrapping TimedeltaArray diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 1aa9b43197b30..c0cbda54ae88e 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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]: """ @@ -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 @@ -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 """ @@ -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 @@ -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] def _get_format_timedelta64( - values: Union[ndarray, TimedeltaIndex, TimedeltaArray], + values: Union[np.ndarray, TimedeltaIndex, TimedeltaArray], nat_rep: str = "NaT", box: bool = False, ) -> Callable: @@ -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 @@ -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. @@ -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. @@ -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: @@ -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