Skip to content

Commit 8f0e263

Browse files
authored
REF: simplify dt64 formatter fucntions (#37907)
1 parent 7f8bf8e commit 8f0e263

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def _formatter_func(self):
374374
from pandas.io.formats.format import get_format_datetime64
375375

376376
formatter = get_format_datetime64(is_dates_only=self._is_dates_only)
377-
return lambda x: f"'{formatter(x, tz=self.tz)}'"
377+
return lambda x: f"'{formatter(x)}'"
378378

379379
# --------------------------------------------------------------------
380380
# Set Operation Methods

pandas/io/formats/format.py

+12-25
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from contextlib import contextmanager
77
from csv import QUOTE_NONE, QUOTE_NONNUMERIC
8-
from datetime import tzinfo
98
import decimal
109
from functools import partial
1110
from io import StringIO
@@ -36,7 +35,6 @@
3635

3736
from pandas._libs import lib
3837
from pandas._libs.missing import NA
39-
from pandas._libs.tslib import format_array_from_datetime
4038
from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
4139
from pandas._libs.tslibs.nattype import NaTType
4240
from pandas._typing import (
@@ -1529,11 +1527,9 @@ def _format_strings(self) -> List[str]:
15291527
if self.formatter is not None and callable(self.formatter):
15301528
return [self.formatter(x) for x in values]
15311529

1532-
fmt_values = format_array_from_datetime(
1533-
values.asi8.ravel(),
1534-
format=get_format_datetime64_from_values(values, self.date_format),
1535-
na_rep=self.nat_rep,
1536-
).reshape(values.shape)
1530+
fmt_values = values._data._format_native_types(
1531+
na_rep=self.nat_rep, date_format=self.date_format
1532+
)
15371533
return fmt_values.tolist()
15381534

15391535

@@ -1653,46 +1649,37 @@ def is_dates_only(
16531649
return False
16541650

16551651

1656-
def _format_datetime64(
1657-
x: Union[NaTType, Timestamp], tz: Optional[tzinfo] = None, nat_rep: str = "NaT"
1658-
) -> str:
1659-
if x is None or (is_scalar(x) and isna(x)):
1652+
def _format_datetime64(x: Union[NaTType, Timestamp], nat_rep: str = "NaT") -> str:
1653+
if x is NaT:
16601654
return nat_rep
16611655

1662-
if tz is not None or not isinstance(x, Timestamp):
1663-
if getattr(x, "tzinfo", None) is not None:
1664-
x = Timestamp(x).tz_convert(tz)
1665-
else:
1666-
x = Timestamp(x).tz_localize(tz)
1667-
16681656
return str(x)
16691657

16701658

16711659
def _format_datetime64_dateonly(
1672-
x: Union[NaTType, Timestamp], nat_rep: str = "NaT", date_format: None = None
1660+
x: Union[NaTType, Timestamp],
1661+
nat_rep: str = "NaT",
1662+
date_format: Optional[str] = None,
16731663
) -> str:
1674-
if x is None or (is_scalar(x) and isna(x)):
1664+
if x is NaT:
16751665
return nat_rep
16761666

1677-
if not isinstance(x, Timestamp):
1678-
x = Timestamp(x)
1679-
16801667
if date_format:
16811668
return x.strftime(date_format)
16821669
else:
16831670
return x._date_repr
16841671

16851672

16861673
def get_format_datetime64(
1687-
is_dates_only: bool, nat_rep: str = "NaT", date_format: None = None
1674+
is_dates_only: bool, nat_rep: str = "NaT", date_format: Optional[str] = None
16881675
) -> Callable:
16891676

16901677
if is_dates_only:
1691-
return lambda x, tz=None: _format_datetime64_dateonly(
1678+
return lambda x: _format_datetime64_dateonly(
16921679
x, nat_rep=nat_rep, date_format=date_format
16931680
)
16941681
else:
1695-
return lambda x, tz=None: _format_datetime64(x, tz=tz, nat_rep=nat_rep)
1682+
return lambda x: _format_datetime64(x, nat_rep=nat_rep)
16961683

16971684

16981685
def get_format_datetime64_from_values(

0 commit comments

Comments
 (0)