-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle datetime arrays with timezone information? #3656
Comments
I feel we should go with option 1, so the behavior is consistent with both GMT and the Python world (at least for matplotlib and pandas). import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
{
"datetime": pd.date_range(start="2024-01-01", periods=10, freq="H").tz_localize("Asia/Shanghai"),
"y": np.random.rand(10),
}
)
print(df["datetime"])
plt.figure(figsize=(10, 6))
plt.plot(df["datetime"], df["y"], marker="o", linestyle="-", color="blue", label="y values")
plt.title("Time Series Data (Hourly)")
plt.xlabel("Datetime (UTC+8)")
plt.ylabel("Y")
plt.show()
# Use pandas' plotting function
df.plot(
x="datetime",
y="y",
kind="line",
marker="o",
title="Time Series Data (Hourly)",
figsize=(10, 6),
grid=True,
xlabel="Datetime (UTC+8)",
ylabel="Y Values",
legend=True
)
plt.show() |
Yep, was typing this out too quickly. Have updated the top-post to clarify this.
I'm trying to get some historical context on how matplotlib/pandas has handled timezone plotting before. It seems like matplotlib used to have a There's also an interesting case on what to do when plotting two or more arrays with different timezones (e.g. UTC and Europe/Berlin time) at matplotlib/matplotlib#8072 (comment). With option 2, everything is converted to UTC so things will be plotted 'correctly'; but with option 1, ignoring the timezones means the data would be plotted in a non-deterministic manner depending on which array (UTC or Europe/Berlin) was passed in first. |
Isn't it clear that matplotlib's
This won't be a case in GMT or PyGMT, since arrays can be processed independently. What makes more troubles is the handling of datetime with different timezones in arguments (although people are unlikely and unrecommended to use mixed timezones) like:
|
Originally posted by @weiji14 in #3621 (comment)
datetime
Context is that we need to decide on whether users would expect that datetime arrays/types (e.g. Python
datetime.datetime
,pandas.Series
withpandas.Timestamp
,pyarrow.timestamp
, etc) that have non-UTC timezones should be plotted in the same non-UTC timezone, or have an automated conversion to a UTC timezone before plotting.Vote 🚀 for option 1, and 👀 for option 2. Do comment down below to explain.
The text was updated successfully, but these errors were encountered: