Skip to content

Commit c093e86

Browse files
author
Sylvain MARIE
committed
Blackened
1 parent 7b74bd2 commit c093e86

File tree

9 files changed

+125
-59
lines changed

9 files changed

+125
-59
lines changed

pandas/_libs/tslibs/strftime.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44

55
class UnsupportedStrFmtDirective(ValueError):
6-
"""The format contains a directive that is not supported in this context.
7-
"""
6+
"""The format contains a directive that is not supported in this context."""
87

98

109
_COMMON_UNSUPPORTED = (
@@ -32,25 +31,28 @@ class UnsupportedStrFmtDirective(ValueError):
3231

3332

3433
_COMMON_MAP = {
35-
"%d": ("day", "02d"), # Day of the month as a zero-padded decimal number.
34+
"%d": ("day", "02d"), # Day of the month as a zero-padded decimal number.
3635
"%m": ("month", "02d"), # Month as a zero-padded decimal number.
37-
"%Y": ("year", "d"), # Year with century as a decimal number.
38-
"%H": ("hour", "02d"), # Hour (24-hour clock) as a zero-padded decimal number.
39-
"%M": ("min", "02d"), # Minute as a zero-padded decimal number.
40-
"%S": ("sec", "02d"), # Second as a zero-padded decimal number.
36+
"%Y": ("year", "d"), # Year with century as a decimal number.
37+
"%H": ("hour", "02d"), # Hour (24-hour clock) as a zero-padded decimal number.
38+
"%M": ("min", "02d"), # Minute as a zero-padded decimal number.
39+
"%S": ("sec", "02d"), # Second as a zero-padded decimal number.
4140
}
4241

4342
_DATETIME_MAP = {
44-
"%f": ("us", "06d"), # Microsecond as a decimal number, zero-padded to 6 digits.
43+
"%f": ("us", "06d"), # Microsecond as a decimal number, zero-padded to 6 digits.
4544
}
4645

4746
_PERIOD_MAP = {
48-
"%f": ("fyear", "02d"), # 'Fiscal' year without century as zero-padded decimal number [00,99]
49-
"%F": ("Fyear", "d"), # 'Fiscal' year with century as a decimal number
50-
"%q": ("q", "d"), # Quarter as a decimal number [1,4]
51-
"%l": ("ms", "03d"), # Microsecond as a decimal number, zero-padded to 3 digits.
52-
"%u": ("us", "06d"), # Microsecond as a decimal number, zero-padded to 6 digits.
53-
"%n": ("ns", "09d"), # Microsecond as a decimal number, zero-padded to 9 digits.
47+
"%f": (
48+
"fyear",
49+
"02d",
50+
), # 'Fiscal' year without century as zero-padded decimal number [00,99]
51+
"%F": ("Fyear", "d"), # 'Fiscal' year with century as a decimal number
52+
"%q": ("q", "d"), # Quarter as a decimal number [1,4]
53+
"%l": ("ms", "03d"), # Microsecond as a decimal number, zero-padded to 3 digits.
54+
"%u": ("us", "06d"), # Microsecond as a decimal number, zero-padded to 6 digits.
55+
"%n": ("ns", "09d"), # Microsecond as a decimal number, zero-padded to 9 digits.
5456
}
5557

5658

@@ -153,7 +155,9 @@ def convert_strftime_format(
153155
for _map in directive_maps:
154156
for key, (_name, _fmt) in _map.items():
155157
# for example replace "%d" by "{day:02d}" but with escaped { and }
156-
strftime_fmt = strftime_fmt.replace(key, f"{esc_l}{_name}:{_fmt}{esc_r}")
158+
strftime_fmt = strftime_fmt.replace(
159+
key, f"{esc_l}{_name}:{_fmt}{esc_r}"
160+
)
157161

158162
# Restore the %% into %
159163
strftime_fmt = strftime_fmt.replace(esc, "%")

pandas/core/arrays/datetimelike.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ def asi8(self) -> npt.NDArray[np.int64]:
294294
# ----------------------------------------------------------------
295295
# Rendering Methods
296296

297-
def _format_native_types(self, *, na_rep="NaT", date_format=None, fast_strftime=True):
297+
def _format_native_types(
298+
self, *, na_rep="NaT", date_format=None, fast_strftime=True
299+
):
298300
"""
299301
Helper method for astype when converting to strings.
300302
@@ -1589,7 +1591,9 @@ class DatelikeOps(DatetimeLikeArrayMixin):
15891591
URL="https://docs.python.org/3/library/datetime.html"
15901592
"#strftime-and-strptime-behavior"
15911593
)
1592-
def strftime(self, date_format: str, fast_strftime: bool = True) -> npt.NDArray[np.object_]:
1594+
def strftime(
1595+
self, date_format: str, fast_strftime: bool = True
1596+
) -> npt.NDArray[np.object_]:
15931597
"""
15941598
Convert to Index using specified date_format.
15951599
@@ -1630,7 +1634,9 @@ def strftime(self, date_format: str, fast_strftime: bool = True) -> npt.NDArray[
16301634
'March 10, 2018, 09:00:02 AM'],
16311635
dtype='object')
16321636
"""
1633-
result = self._format_native_types(date_format=date_format, na_rep=np.nan, fast_strftime=fast_strftime)
1637+
result = self._format_native_types(
1638+
date_format=date_format, na_rep=np.nan, fast_strftime=fast_strftime
1639+
)
16341640
return result.astype(object, copy=False)
16351641

16361642

pandas/core/arrays/datetimes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ def _format_native_types(
680680
fmt = get_format_datetime64_from_values(self, date_format)
681681

682682
return tslib.format_array_from_datetime(
683-
self.asi8, tz=self.tz, format=fmt, na_rep=na_rep,
683+
self.asi8,
684+
tz=self.tz,
685+
format=fmt,
686+
na_rep=na_rep,
684687
fast_strftime=fast_strftime,
685688
)
686689

pandas/core/indexes/datetimelike.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,22 @@ def format(
176176
if formatter is not None:
177177
return header + list(self.map(formatter))
178178

179-
return self._format_with_header(header, na_rep=na_rep, date_format=date_format, fast_strftime=fast_strftime)
179+
return self._format_with_header(
180+
header, na_rep=na_rep, date_format=date_format, fast_strftime=fast_strftime
181+
)
180182

181183
def _format_with_header(
182-
self, header: list[str], na_rep: str = "NaT", date_format: str | None = None, fast_strftime: bool = True,
184+
self,
185+
header: list[str],
186+
na_rep: str = "NaT",
187+
date_format: str | None = None,
188+
fast_strftime: bool = True,
183189
) -> list[str]:
184190
# matches base class except for whitespace padding and date_format
185191
return header + list(
186-
self._format_native_types(na_rep=na_rep, date_format=date_format, fast_strftime=fast_strftime)
192+
self._format_native_types(
193+
na_rep=na_rep, date_format=date_format, fast_strftime=fast_strftime
194+
)
187195
)
188196

189197
@property

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):
259259
# methods that dispatch to DatetimeArray and wrap result
260260

261261
@doc(DatetimeArray.strftime)
262-
def strftime(self, date_format, fast_strftime : bool = True) -> Index:
262+
def strftime(self, date_format, fast_strftime: bool = True) -> Index:
263263
arr = self._data.strftime(date_format, fast_strftime=fast_strftime)
264264
return Index(arr, name=self.name)
265265

pandas/core/tools/datetimes.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,13 @@ def fast_strftime(
13161316
"""
13171317
# common dict used for formatting
13181318
fmt_dct = dict(
1319-
year=dt.year, month=dt.month, day=dt.day, hour=dt.hour,
1320-
min=dt.minute, sec=dt.second, us=dt.microsecond
1319+
year=dt.year,
1320+
month=dt.month,
1321+
day=dt.day,
1322+
hour=dt.hour,
1323+
min=dt.minute,
1324+
sec=dt.second,
1325+
us=dt.microsecond,
13211326
)
13221327

13231328
# get the formatting template

pandas/io/formats/format.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1634,8 +1634,9 @@ def _format_strings(self) -> list[str]:
16341634
return [self.formatter(x) for x in values]
16351635

16361636
fmt_values = values._data._format_native_types(
1637-
na_rep=self.nat_rep, date_format=self.date_format,
1638-
fast_strftime=self.fast_strftime
1637+
na_rep=self.nat_rep,
1638+
date_format=self.date_format,
1639+
fast_strftime=self.fast_strftime,
16391640
)
16401641
return fmt_values.tolist()
16411642

@@ -1780,8 +1781,13 @@ def _format_datetime64_dateonly(
17801781
if str_date_fmt:
17811782
# Faster, using string formatting
17821783
return str_date_fmt % dict(
1783-
year=x.year, month=x.month, day=x.day, hour=x.hour,
1784-
min=x.min, sec=x.sec, us=x.us
1784+
year=x.year,
1785+
month=x.month,
1786+
day=x.day,
1787+
hour=x.hour,
1788+
min=x.min,
1789+
sec=x.sec,
1790+
us=x.us,
17851791
)
17861792
else:
17871793
# Slower
@@ -1794,7 +1800,10 @@ def _format_datetime64_dateonly(
17941800

17951801

17961802
def get_format_datetime64(
1797-
is_dates_only: bool, nat_rep: str = "NaT", date_format: str | None = None, fast_strftime : bool = True,
1803+
is_dates_only: bool,
1804+
nat_rep: str = "NaT",
1805+
date_format: str | None = None,
1806+
fast_strftime: bool = True,
17981807
) -> Callable:
17991808
"""Return a formatter callable taking a datetime64 as input and providing
18001809
a string as output"""
@@ -1810,8 +1819,7 @@ def get_format_datetime64(
18101819
pass
18111820

18121821
return lambda x: _format_datetime64_dateonly(
1813-
x, nat_rep=nat_rep, date_format=date_format,
1814-
str_date_fmt=str_date_fmt
1822+
x, nat_rep=nat_rep, date_format=date_format, str_date_fmt=str_date_fmt
18151823
)
18161824
else:
18171825
return lambda x: _format_datetime64(x, nat_rep=nat_rep)

pandas/tests/io/formats/test_format.py

+58-26
Original file line numberDiff line numberDiff line change
@@ -3188,10 +3188,16 @@ def test_datetime_tz_custom(self):
31883188
# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
31893189
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)
31903190
# If tz is currently set as utc, we'll see 2012
3191-
assert dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0] == "2012-12-31__foo__23:00:00"
3191+
assert (
3192+
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
3193+
== "2012-12-31__foo__23:00:00"
3194+
)
31923195
# If tz is currently set as paris, we'll see 2013
31933196
dt = dt.tz_convert("Europe/Paris")
3194-
assert dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0] == "2013-01-01__foo__00:00:00"
3197+
assert (
3198+
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
3199+
== "2013-01-01__foo__00:00:00"
3200+
)
31953201

31963202
def test_date(self):
31973203
formatted = pd.to_datetime([datetime(2003, 1, 1), NaT]).format()
@@ -3279,12 +3285,15 @@ class TestDatetimeFastFormatter:
32793285
kind of date/time objects.
32803286
"""
32813287

3282-
@pytest.mark.parametrize("strftime_format", (
3283-
"%Y-%m-%d %H:%M:%S",
3284-
"%Y%%Y",
3285-
"%Y{%Y}",
3286-
"%Y-%m-%dT%H:%M:%S.%fZ",
3287-
))
3288+
@pytest.mark.parametrize(
3289+
"strftime_format",
3290+
(
3291+
"%Y-%m-%d %H:%M:%S",
3292+
"%Y%%Y",
3293+
"%Y{%Y}",
3294+
"%Y-%m-%dT%H:%M:%S.%fZ",
3295+
),
3296+
)
32883297
@pytest.mark.parametrize("new_style", (False, True))
32893298
def test_fast_strftime_basic(self, strftime_format, new_style):
32903299
"""Test that formatting standard `datetime` objects with our utils works as good as strftime."""
@@ -3297,8 +3306,13 @@ def test_fast_strftime_basic(self, strftime_format, new_style):
32973306

32983307
# common dict used for formatting
32993308
fmt_dct = dict(
3300-
year=dt.year, month=dt.month, day=dt.day, hour=dt.hour,
3301-
min=dt.minute, sec=dt.second, us=dt.microsecond
3309+
year=dt.year,
3310+
month=dt.month,
3311+
day=dt.day,
3312+
hour=dt.hour,
3313+
min=dt.minute,
3314+
sec=dt.second,
3315+
us=dt.microsecond,
33023316
)
33033317

33043318
# get the formatting string
@@ -3315,11 +3329,14 @@ def test_fast_strftime_basic(self, strftime_format, new_style):
33153329
res2 = fast_strftime(dt, strftime_format, new_style_fmt=new_style)
33163330
assert res2 == res
33173331

3318-
@pytest.mark.parametrize("strftime_format", (
3319-
"%Y-%m-%d %H:%M:%S",
3320-
"%Y %Y",
3321-
"%Y-%m-%dT%H:%M:%S.%fZ",
3322-
))
3332+
@pytest.mark.parametrize(
3333+
"strftime_format",
3334+
(
3335+
"%Y-%m-%d %H:%M:%S",
3336+
"%Y %Y",
3337+
"%Y-%m-%dT%H:%M:%S.%fZ",
3338+
),
3339+
)
33233340
def test_fast_strftime_perf(self, strftime_format):
33243341
"""Test that formatting standard `datetime` objects with our utils is faster than strftime."""
33253342

@@ -3332,8 +3349,13 @@ def test_fast_strftime_perf(self, strftime_format):
33323349

33333350
# Perf comparison: confirm the results in https://stackoverflow.com/a/43495629/7262247
33343351
fmt_dct = dict(
3335-
year=dt.year, month=dt.month, day=dt.day, hour=dt.hour,
3336-
min=dt.minute, sec=dt.second, us=dt.microsecond
3352+
year=dt.year,
3353+
month=dt.month,
3354+
day=dt.day,
3355+
hour=dt.hour,
3356+
min=dt.minute,
3357+
sec=dt.second,
3358+
us=dt.microsecond,
33373359
)
33383360

33393361
glob = globals()
@@ -3350,7 +3372,7 @@ def test_fast_strftime_perf(self, strftime_format):
33503372
Timer("old_style_format % fmt_dct", globals=glob).repeat(7, 1000)
33513373
)
33523374
# Out[5]: 0.0030
3353-
assert new_style_best < strftime_best # much better
3375+
assert new_style_best < strftime_best # much better
33543376
assert old_style_best < new_style_best # even better !
33553377

33563378
def test_bad_strftime_directive(self):
@@ -3364,21 +3386,31 @@ def test_bad_strftime_directive(self):
33643386
# We align with the same behaviour
33653387
x.dt.strftime(date_format="%Y-%M-%D___")
33663388

3367-
@pytest.mark.parametrize("date_format", (
3368-
# note: "%Y-%m-%d %H:%M:%S and "%Y-%m-%d %H:%M:%S.%f are always accelerated (hardcoded)
3369-
"%Y-%m-%d__foo__%H:%M:%S",
3370-
"%Y %Y",
3371-
"%Y-%m-%dT%H:%M:%S.%fZ",
3372-
))
3389+
@pytest.mark.parametrize(
3390+
"date_format",
3391+
(
3392+
# note: "%Y-%m-%d %H:%M:%S and "%Y-%m-%d %H:%M:%S.%f are always accelerated (hardcoded)
3393+
"%Y-%m-%d__foo__%H:%M:%S",
3394+
"%Y %Y",
3395+
"%Y-%m-%dT%H:%M:%S.%fZ",
3396+
),
3397+
)
33733398
def test_perf_datetime64_strftime(self, date_format):
33743399
x = Series(date_range("20130101 09:00:00", periods=100, freq="min"))
33753400
# res = x.dt.strftime(date_format=date_format)
33763401
# slow_res = x.dt.strftime(date_format=date_format, fast_strftime=False)
33773402

33783403
glob = globals()
33793404
glob.update(locals())
3380-
fast_best = min(Timer("x.dt.strftime(date_format=date_format)", globals=glob).repeat(3, 100))
3381-
strftime_best = min(Timer("x.dt.strftime(date_format=date_format, fast_strftime=False)", globals=glob).repeat(3, 100))
3405+
fast_best = min(
3406+
Timer("x.dt.strftime(date_format=date_format)", globals=glob).repeat(3, 100)
3407+
)
3408+
strftime_best = min(
3409+
Timer(
3410+
"x.dt.strftime(date_format=date_format, fast_strftime=False)",
3411+
globals=glob,
3412+
).repeat(3, 100)
3413+
)
33823414
assert fast_best < strftime_best # much better
33833415

33843416
# How many alternative are worth doing here ?

pandas/tests/scalar/test_nat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def test_nat_iso_format(get_nat):
198198
"normalize",
199199
"to_julian_date",
200200
"to_period",
201-
"tz"
201+
"tz",
202202
]
203203
),
204204
(

0 commit comments

Comments
 (0)