Skip to content

Commit ec8036c

Browse files
author
Sylvain MARIE
committed
Re applied mods to TestDatetimeIndexFormat, since it had moved somewhere else and was renamed since pandas-dev#55603
1 parent 21b0a88 commit ec8036c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

pandas/tests/indexes/datetimes/test_formats.py

+49
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,52 @@ def test_format_date_explicit_date_format(self):
354354
formatted = dti.format(date_format="%m-%d-%Y", na_rep="UT")
355355
assert formatted[0] == "02-01-2003"
356356
assert formatted[1] == "UT"
357+
358+
def test_format_datetime_tz(self):
359+
"""Test default `format()` with tz-aware datetime index."""
360+
# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
361+
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)
362+
# Since tz is currently set as utc, we'll see 2012
363+
msg = "DatetimeIndex.format is deprecated"
364+
with tm.assert_produces_warning(FutureWarning, match=msg):
365+
assert dt.format()[0] == "2012-12-31 23:00:00+00:00"
366+
# If we set tz as paris, we'll see 2013
367+
dt = dt.tz_convert("Europe/Paris")
368+
with tm.assert_produces_warning(FutureWarning, match=msg):
369+
assert dt.format()[0] == "2013-01-01 00:00:00+01:00"
370+
371+
def test_format_datetime_tz_explicit(self):
372+
"""Test `format()` with tz-aware dt and a custom format string."""
373+
# Get locale-specific reference
374+
am_local, pm_local = get_local_am_pm()
375+
376+
# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
377+
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)
378+
379+
# If tz is currently set as utc, we'll see 2012
380+
msg = "DatetimeIndex.format is deprecated"
381+
with tm.assert_produces_warning(FutureWarning, match=msg):
382+
assert (
383+
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
384+
== "2012-12-31__foo__23:00:00"
385+
)
386+
# same with fancy format
387+
with tm.assert_produces_warning(FutureWarning, match=msg):
388+
assert (
389+
dt.format(date_format="20%y-%m-%d__foo__%I:%M:%S%p")[0]
390+
== f"2012-12-31__foo__11:00:00{pm_local}"
391+
)
392+
393+
# If tz is currently set as paris, we'll see 2013
394+
dt = dt.tz_convert("Europe/Paris")
395+
with tm.assert_produces_warning(FutureWarning, match=msg):
396+
assert (
397+
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
398+
== "2013-01-01__foo__00:00:00"
399+
)
400+
# same with fancy format
401+
with tm.assert_produces_warning(FutureWarning, match=msg):
402+
assert (
403+
dt.format(date_format="20%y-%m-%d__foo__%I:%M:%S%p")[0]
404+
== f"2013-01-01__foo__12:00:00{am_local}"
405+
)

0 commit comments

Comments
 (0)