Skip to content

Commit 1a22f9d

Browse files
simonjayhawkinsjreback
authored andcommitted
CLN: pandas\io\formats\format.py (#27577)
1 parent c1fd9ab commit 1a22f9d

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

pandas/core/indexes/timedeltas.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,11 @@ def _formatter_func(self):
299299
def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
300300
from pandas.io.formats.format import Timedelta64Formatter
301301

302-
return Timedelta64Formatter(
303-
values=self, nat_rep=na_rep, justify="all"
304-
).get_result()
302+
return np.asarray(
303+
Timedelta64Formatter(
304+
values=self, nat_rep=na_rep, justify="all"
305+
).get_result()
306+
)
305307

306308
# -------------------------------------------------------------------
307309
# Wrapping TimedeltaArray

pandas/io/formats/format.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from dateutil.tz.tz import tzutc
2727
from dateutil.zoneinfo import tzfile
2828
import numpy as np
29-
from numpy import float64, int32, ndarray
3029

3130
from pandas._config.config import get_option, set_option
3231

@@ -1125,7 +1124,7 @@ def __init__(
11251124
self.fixed_width = fixed_width
11261125
self.leading_space = leading_space
11271126

1128-
def get_result(self) -> Union[ndarray, List[str]]:
1127+
def get_result(self) -> List[str]:
11291128
fmt_values = self._format_strings()
11301129
return _make_fixed_width(fmt_values, self.justify)
11311130

@@ -1261,7 +1260,7 @@ def formatter(value):
12611260

12621261
return formatter
12631262

1264-
def get_result_as_array(self) -> Union[ndarray, List[str]]:
1263+
def get_result_as_array(self) -> np.ndarray:
12651264
"""
12661265
Returns the float values converted into strings using
12671266
the parameters given at initialisation, as a numpy array
@@ -1301,9 +1300,10 @@ def format_values_with(float_format):
13011300

13021301
if self.fixed_width:
13031302
if is_complex:
1304-
return _trim_zeros_complex(values, na_rep)
1303+
result = _trim_zeros_complex(values, na_rep)
13051304
else:
1306-
return _trim_zeros_float(values, na_rep)
1305+
result = _trim_zeros_float(values, na_rep)
1306+
return np.asarray(result, dtype="object")
13071307

13081308
return values
13091309

@@ -1368,7 +1368,7 @@ def _format_strings(self) -> List[str]:
13681368
class Datetime64Formatter(GenericArrayFormatter):
13691369
def __init__(
13701370
self,
1371-
values: Union[ndarray, "Series", DatetimeIndex, DatetimeArray],
1371+
values: Union[np.ndarray, "Series", DatetimeIndex, DatetimeArray],
13721372
nat_rep: str = "NaT",
13731373
date_format: None = None,
13741374
**kwargs
@@ -1425,7 +1425,7 @@ def _format_strings(self) -> List[str]:
14251425

14261426
def format_percentiles(
14271427
percentiles: Union[
1428-
ndarray, List[Union[int, float]], List[float], List[Union[str, float]]
1428+
np.ndarray, List[Union[int, float]], List[float], List[Union[str, float]]
14291429
]
14301430
) -> List[str]:
14311431
"""
@@ -1493,7 +1493,9 @@ def format_percentiles(
14931493
return [i + "%" for i in out]
14941494

14951495

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

@@ -1557,7 +1559,7 @@ def _get_format_datetime64(
15571559

15581560

15591561
def _get_format_datetime64_from_values(
1560-
values: Union[ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str]
1562+
values: Union[np.ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str]
15611563
) -> Optional[str]:
15621564
""" given values and a date_format, return a string format """
15631565

@@ -1589,7 +1591,7 @@ def _format_strings(self) -> List[str]:
15891591
class Timedelta64Formatter(GenericArrayFormatter):
15901592
def __init__(
15911593
self,
1592-
values: Union[ndarray, TimedeltaIndex],
1594+
values: Union[np.ndarray, TimedeltaIndex],
15931595
nat_rep: str = "NaT",
15941596
box: bool = False,
15951597
**kwargs
@@ -1598,16 +1600,15 @@ def __init__(
15981600
self.nat_rep = nat_rep
15991601
self.box = box
16001602

1601-
def _format_strings(self) -> ndarray:
1603+
def _format_strings(self) -> List[str]:
16021604
formatter = self.formatter or _get_format_timedelta64(
16031605
self.values, nat_rep=self.nat_rep, box=self.box
16041606
)
1605-
fmt_values = np.array([formatter(x) for x in self.values])
1606-
return fmt_values
1607+
return [formatter(x) for x in self.values]
16071608

16081609

16091610
def _get_format_timedelta64(
1610-
values: Union[ndarray, TimedeltaIndex, TimedeltaArray],
1611+
values: Union[np.ndarray, TimedeltaIndex, TimedeltaArray],
16111612
nat_rep: str = "NaT",
16121613
box: bool = False,
16131614
) -> Callable:
@@ -1652,11 +1653,11 @@ def _formatter(x):
16521653

16531654

16541655
def _make_fixed_width(
1655-
strings: Union[ndarray, List[str]],
1656+
strings: List[str],
16561657
justify: str = "right",
16571658
minimum: Optional[int] = None,
16581659
adj: Optional[TextAdjustment] = None,
1659-
) -> Union[ndarray, List[str]]:
1660+
) -> List[str]:
16601661

16611662
if len(strings) == 0 or justify == "all":
16621663
return strings
@@ -1684,7 +1685,7 @@ def just(x):
16841685
return result
16851686

16861687

1687-
def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str]:
1688+
def _trim_zeros_complex(str_complexes: np.ndarray, na_rep: str = "NaN") -> List[str]:
16881689
"""
16891690
Separates the real and imaginary parts from the complex number, and
16901691
executes the _trim_zeros_float method on each of those.
@@ -1696,7 +1697,7 @@ def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str
16961697

16971698

16981699
def _trim_zeros_float(
1699-
str_floats: Union[ndarray, List[str]], na_rep: str = "NaN"
1700+
str_floats: Union[np.ndarray, List[str]], na_rep: str = "NaN"
17001701
) -> List[str]:
17011702
"""
17021703
Trims zeros, leaving just one before the decimal points if need be.
@@ -1760,7 +1761,7 @@ def __init__(self, accuracy: Optional[int] = None, use_eng_prefix: bool = False)
17601761
self.accuracy = accuracy
17611762
self.use_eng_prefix = use_eng_prefix
17621763

1763-
def __call__(self, num: Union[float64, int, float]) -> str:
1764+
def __call__(self, num: Union[int, float]) -> str:
17641765
""" Formats a number in engineering notation, appending a letter
17651766
representing the power of 1000 of the original number. Some examples:
17661767
@@ -1840,7 +1841,7 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non
18401841
set_option("display.column_space", max(12, accuracy + 9))
18411842

18421843

1843-
def _binify(cols: List[int32], line_width: Union[int32, int]) -> List[int]:
1844+
def _binify(cols: List[np.int32], line_width: Union[np.int32, int]) -> List[int]:
18441845
adjoin_width = 1
18451846
bins = []
18461847
curr_width = 0

0 commit comments

Comments
 (0)