@@ -239,31 +239,78 @@ def test_period_format_and_strftime_default(self):
239
239
assert formatted [0 ] == "2003-01-01 12:01:01.123456789"
240
240
assert formatted [1 ] == "2003-01-01 12:01:01.123456790"
241
241
242
- def test_period_custom (self ):
242
+ @pytest .mark .parametrize ("fast_strftime" , (False , True ))
243
+ def test_period_custom (self , fast_strftime ):
243
244
# GH#46252 custom formatting directives %l (ms) and %u (us)
244
245
msg = "PeriodIndex.format is deprecated"
245
246
246
247
# 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 " )
248
249
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
+ )
250
254
assert formatted [0 ] == "03 12:01:01 (ms=123 us=123000 ns=123000000)"
251
255
assert formatted [1 ] == "03 12:01:01 (ms=124 us=124000 ns=124000000)"
252
256
253
257
# 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 " )
255
259
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
+ )
257
264
assert formatted [0 ] == "03 12:01:01 (ms=123 us=123456 ns=123456000)"
258
265
assert formatted [1 ] == "03 12:01:01 (ms=123 us=123457 ns=123457000)"
259
266
260
267
# 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 " )
262
269
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
+ )
264
274
assert formatted [0 ] == "03 12:01:01 (ms=123 us=123456 ns=123456789)"
265
275
assert formatted [1 ] == "03 12:01:01 (ms=123 us=123456 ns=123456790)"
266
276
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
+
267
314
def test_period_tz (self ):
268
315
# Formatting periods created from a datetime with timezone.
269
316
msg = r"PeriodIndex\.format is deprecated"
0 commit comments