You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataReader获取雅虎财经USDJPY数据时日期显示异常求助

Fixing Date Offset for Yahoo Finance USDJPY (JPY=X) Data

Hey there, I’ve dealt with this exact date shift issue when pulling forex data from Yahoo Finance before—let’s walk through what’s going on and how to fix it.

What’s Causing the Date Offset?

Yahoo Finance stores forex data timestamps in UTC time, but we’re used to viewing forex dates based on local trading timezones (like Tokyo or New York). That’s why you’re seeing weekend dates (2020-06-14 and 2020-06-21 are Sundays, when forex markets are closed) — the UTC timestamp for a Tokyo-trading-day’s close might still fall on the previous calendar day in UTC. The data itself is correct, but the date labels are shifted back by one day relative to the actual trading day.

Your raw output confirms this:

Date
2020-06-14 107.310997
2020-06-15 107.463997
2020-06-16 107.410004
2020-06-17 106.893997
2020-06-18 107.005997
2020-06-21 106.831001
2020-06-22 106.903000
2020-06-23 106.431999
2020-06-24 107.043999
2020-06-25 107.154999
Name: Close, dtype: float64

Two Simple Fixes

1. Quick Manual Date Shift

If you’re sure the offset is exactly one day, you can directly shift the index forward by one day. Note: I added the missing datetime import to your original code—you’ll need that to use date()!

from pandas_datareader import data
from datetime import date  # Missing import in your original code
from pandas import Series, DataFrame

# Pull raw data
raw_data = data.DataReader('JPY=X', 'yahoo', date(2020,6,15), date(2020,6,28))['Close']

# Shift dates forward by 1 day, drop the final NaN entry
fixed_data = raw_data.shift(-1).dropna()

# Optional: Convert index to date objects (removes time component)
fixed_data.index = fixed_data.index.date

print(fixed_data)

This will map the 2020-06-14 entry to 2020-06-15, eliminate weekend dates, and keep all the correct close prices.

2. Timezone Conversion (More Robust)

For a more reliable fix that accounts for timezone differences properly, convert the UTC timestamps to your target trading timezone (e.g., Tokyo time for USDJPY):

from pandas_datareader import data
from datetime import date
import pytz  # Install first with: pip install pytz

# Pull full dataset (keep datetime index instead of just Close)
df = data.DataReader('JPY=X', 'yahoo', date(2020,6,15), date(2020,6,28))

# Convert UTC index to Tokyo timezone
df.index = df.index.tz_localize('UTC').tz_convert('Asia/Tokyo')

# Resample to daily data (take last Close of each trading day) and drop NaNs
fixed_data = df['Close'].resample('D').last().dropna()

# Extract just the date for the index
fixed_data.index = fixed_data.index.date

print(fixed_data)

This method avoids guesswork about the offset and works for any forex pair tied to a specific timezone.

内容的提问来源于stack exchange,提问作者absk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:27:33