CodexBloom - Programming Q&A Platform

How to merge two DataFrames on a date index while handling timezone-aware datetime objects in Pandas?

👀 Views: 72 💬 Answers: 1 📅 Created: 2025-09-06
pandas dataframe merge Python

I'm building a feature where I'm prototyping a solution and I've hit a wall trying to I'm trying to merge two DataFrames that both have date indices with timezone-aware datetime objects, but I'm running into issues with mismatched timezones... Here’s the situation: I have two DataFrames, `df1` and `df2`, both indexed by dates. `df1` has a timezone of 'UTC' and `df2` is set to 'America/New_York'. When I attempt to merge them, I get `ValueError: Cannot merge with different timezones`. I've tried converting the timezone of `df2` to 'UTC' before merging, but it seems that the index conversion isn't being applied correctly. Here’s the code I've used: ```python import pandas as pd import pytz dates1 = pd.date_range('2023-01-01', periods=5, freq='D', tz='UTC') values1 = [1, 2, 3, 4, 5] df1 = pd.DataFrame(values1, index=dates1, columns=['values']) dates2 = pd.date_range('2023-01-01', periods=5, freq='D', tz='America/New_York') values2 = [10, 20, 30, 40, 50] df2 = pd.DataFrame(values2, index=dates2, columns=['values']) # Attempt to merge merged_df = pd.merge(df1, df2, left_index=True, right_index=True, suffixes=('_utc', '_ny')) ``` When I run this, I get the timezone error. I've also tried normalizing the timezone like so: ```python df2.index = df2.index.tz_convert('UTC') ``` But it still doesn’t work. What’s the proper way to handle merging two DataFrames with timezone-aware indices in Pandas? Is there a best practice for this scenario? Any insights would be greatly appreciated. How would you solve this? This issue appeared after updating to Python stable. Thanks for taking the time to read this! My team is using Python for this desktop app. My development environment is Windows 11.