Skip to content

Commit 21b0a88

Browse files
author
Sylvain MARIE
committed
Re applied mods to TestPeriodIndexFormat, since it had moved somewhere else
1 parent 022c185 commit 21b0a88

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

pandas/tests/indexes/period/test_formats.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,78 @@ def test_period_format_and_strftime_default(self):
239239
assert formatted[0] == "2003-01-01 12:01:01.123456789"
240240
assert formatted[1] == "2003-01-01 12:01:01.123456790"
241241

242-
def test_period_custom(self):
242+
@pytest.mark.parametrize("fast_strftime", (False, True))
243+
def test_period_custom(self, fast_strftime):
243244
# GH#46252 custom formatting directives %l (ms) and %u (us)
244245
msg = "PeriodIndex.format is deprecated"
245246

246247
# 3 digits
247-
per = pd.period_range("2003-01-01 12:01:01.123", periods=2, freq="ms")
248+
per = pd.period_range("2003-01-01 12:01:01.123", periods=2, freq="l")
248249
with tm.assert_produces_warning(FutureWarning, match=msg):
249-
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
250+
formatted = per.format(
251+
date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)",
252+
fast_strftime=fast_strftime,
253+
)
250254
assert formatted[0] == "03 12:01:01 (ms=123 us=123000 ns=123000000)"
251255
assert formatted[1] == "03 12:01:01 (ms=124 us=124000 ns=124000000)"
252256

253257
# 6 digits
254-
per = pd.period_range("2003-01-01 12:01:01.123456", periods=2, freq="us")
258+
per = pd.period_range("2003-01-01 12:01:01.123456", periods=2, freq="u")
255259
with tm.assert_produces_warning(FutureWarning, match=msg):
256-
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
260+
formatted = per.format(
261+
date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)",
262+
fast_strftime=fast_strftime,
263+
)
257264
assert formatted[0] == "03 12:01:01 (ms=123 us=123456 ns=123456000)"
258265
assert formatted[1] == "03 12:01:01 (ms=123 us=123457 ns=123457000)"
259266

260267
# 9 digits
261-
per = pd.period_range("2003-01-01 12:01:01.123456789", periods=2, freq="ns")
268+
per = pd.period_range("2003-01-01 12:01:01.123456789", periods=2, freq="n")
262269
with tm.assert_produces_warning(FutureWarning, match=msg):
263-
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
270+
formatted = per.format(
271+
date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)",
272+
fast_strftime=fast_strftime,
273+
)
264274
assert formatted[0] == "03 12:01:01 (ms=123 us=123456 ns=123456789)"
265275
assert formatted[1] == "03 12:01:01 (ms=123 us=123456 ns=123456790)"
266276

277+
@pytest.mark.parametrize("fast_strftime", (False, True))
278+
@pytest.mark.parametrize(
279+
"locale_str",
280+
[
281+
pytest.param(None, id=str(locale.getlocale())),
282+
"it_IT.utf8",
283+
"it_IT", # Note: encoding will be 'ISO8859-1'
284+
"zh_CN.utf8",
285+
"zh_CN", # Note: encoding will be 'gb2312'
286+
],
287+
)
288+
def test_period_custom_pm(self, fast_strftime, locale_str):
289+
"""Test that using %p in the custom format work well"""
290+
291+
# Skip if locale cannot be set
292+
if locale_str is not None and not tm.can_set_locale(locale_str, locale.LC_ALL):
293+
pytest.skip(f"Skipping as locale '{locale_str}' cannot be set on host.")
294+
295+
# Change locale temporarily for this test.
296+
with tm.set_locale(locale_str, locale.LC_ALL) if locale_str else nullcontext():
297+
# Get locale-specific reference
298+
am_local, pm_local = get_local_am_pm()
299+
300+
# 9 digits
301+
p = pd.period_range("2003-01-01 12:01:01.123456789", periods=2, freq="n")
302+
formatted = p.format(
303+
date_format="%y %I:%M:%S%p (ms=%l us=%u ns=%n)",
304+
fast_strftime=fast_strftime,
305+
)
306+
assert (
307+
formatted[0] == f"03 12:01:01{pm_local} (ms=123 us=123456 ns=123456789)"
308+
)
309+
assert (
310+
formatted[1] == f"03 12:01:01{pm_local} (ms=123 us=123456 ns=123456790)"
311+
)
312+
# fmt: on
313+
267314
def test_period_tz(self):
268315
# Formatting periods created from a datetime with timezone.
269316
msg = r"PeriodIndex\.format is deprecated"

0 commit comments

Comments
 (0)