Skip to content

Commit

Permalink
Re applied mods to TestDatetimeIndexFormat, since it had moved some…
Browse files Browse the repository at this point in the history
…where else and was renamed since pandas-dev#55603
  • Loading branch information
Sylvain MARIE committed Nov 11, 2023
1 parent 21b0a88 commit ec8036c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pandas/tests/indexes/datetimes/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,52 @@ def test_format_date_explicit_date_format(self):
formatted = dti.format(date_format="%m-%d-%Y", na_rep="UT")
assert formatted[0] == "02-01-2003"
assert formatted[1] == "UT"

def test_format_datetime_tz(self):
"""Test default `format()` with tz-aware datetime index."""
# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)
# Since tz is currently set as utc, we'll see 2012
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert dt.format()[0] == "2012-12-31 23:00:00+00:00"
# If we set tz as paris, we'll see 2013
dt = dt.tz_convert("Europe/Paris")
with tm.assert_produces_warning(FutureWarning, match=msg):
assert dt.format()[0] == "2013-01-01 00:00:00+01:00"

def test_format_datetime_tz_explicit(self):
"""Test `format()` with tz-aware dt and a custom format string."""
# Get locale-specific reference
am_local, pm_local = get_local_am_pm()

# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)

# If tz is currently set as utc, we'll see 2012
msg = "DatetimeIndex.format is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert (
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
== "2012-12-31__foo__23:00:00"
)
# same with fancy format
with tm.assert_produces_warning(FutureWarning, match=msg):
assert (
dt.format(date_format="20%y-%m-%d__foo__%I:%M:%S%p")[0]
== f"2012-12-31__foo__11:00:00{pm_local}"
)

# If tz is currently set as paris, we'll see 2013
dt = dt.tz_convert("Europe/Paris")
with tm.assert_produces_warning(FutureWarning, match=msg):
assert (
dt.format(date_format="%Y-%m-%d__foo__%H:%M:%S")[0]
== "2013-01-01__foo__00:00:00"
)
# same with fancy format
with tm.assert_produces_warning(FutureWarning, match=msg):
assert (
dt.format(date_format="20%y-%m-%d__foo__%I:%M:%S%p")[0]
== f"2013-01-01__foo__12:00:00{am_local}"
)

0 comments on commit ec8036c

Please sign in to comment.