从外部数据源接收并对齐以下数据。
open high low close volume
timestamp
2019-04-02 05:59:00 381.00 381.00 379.70 379.70 0
2019-04-02 05:58:00 380.90 380.90 380.85 380.85 5040
2019-04-02 05:57:00 380.85 380.95 380.65 380.95 9615
2019-04-02 05:56:00 380.60 381.20 380.60 381.00 13041
2019-04-02 05:55:00 379.80 380.60 379.80 380.60 19586
import pandas as pd
import sys
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
csvdata = StringIO("""timestamp,open,high,low,close,volume
2019-04-02 05:59:00,381.00,381.00,379.70,379.70,0
2019-04-02 05:58:00,380.90,380.90,380.85,380.85,5040
2019-04-02 05:57:00,380.85,380.95,380.65,380.95,9615
2019-04-02 05:56:00,380.60,381.20,380.60,381.00,13041
2019-04-02 05:55:00,379.80,380.60,379.80,380.60,19586""")
df = pd.read_csv(csvdata, sep=",", index_col="timestamp", parse_dates=True, infer_datetime_format=True)
# results
print( df)
此代码适用于其他计算,但此处显示的时间戳是GMT-4时间戳(从数据提供者处了解)。我希望将其从一个时区转换为其他时区GMT-4到GMT + 5.30。尝试了一些选项并且失败显示“日期”不是索引的一部分。如何将此作为可重复使用的代码,从任何时区(输入时区可能更改为UTC或GMT-2)转换为所需的时区。
#fmt = "%d-%m-%Y %H:%M:%S %Z%z"
#now.strftime('%m-%d-%y %H:%M:%S')
#x.strftime("%Y-%m-%dT%H:%M:%S%Z") #'2015-03-26T10:58:51'
fmt = "%d-%m-%Y %H:%M"
#keyupdate=key
dfdaily = pd.read_csv(dailyurl, index_col=[0], parse_dates=[0])
#dfdaily['date'] = pd.to_datetime(dfdaily['date'])
print(dfdaily.head(5))
dfdaily = dfdaily.rename_axis('date')
dfdaily= dfdaily.sort_index()
dfdaily.index.name = 'date'
print("Actual Time",dfdaily.index.strftime(fmt))
# Convert to US/Pacific time zone
now_pacific = dfdaily.index.astimezone(timezone('US/Pacific'))
print("Coverted US time",now_pacific['date'].strftime(fmt))
# Convert to Europe/Berlin time zone
now_india = now_pacific.index.astimezone(timezone('Asia/Kolkata'))
print("India Time",now_india['date'].strftime(fmt))
return dfdaily
**注意:**当我调试并查看检索到的数据时,它显示2019-04-02T0205:59:00.0000但是当我打印时它出现在2019-04-02 05:59:00为什么?打印(dfdaily.head(5))
在上面的代码中,我需要做什么来转换时区。对我来说,索引没有工作的日期也不知道为什么。
这里的技巧是输入数据。根据某些事情,日期时间数据将加载为“天真”(IE no tz)或“tz-aware”。鉴于上述代码中的MCVE,预计数据加载“天真”。通过查看最初加载的索引进行检查。
df.index.tz
一旦使用DateTimeIndex
构建数据帧,Pandas就可以完成所需的tz工作。
在下面的答案中,请注意Olson TZ数据库用于GMT + X小时偏移的地名。 GMT + 5:30是亚洲/加尔各答。
此外,对于时区和日期时间类型的任何高级操作,实际上需要pytz
库。
import pandas as pd
import sys
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
csvdata = StringIO("""timestamp,open,high,low,close,volume
2019-04-02 05:59:00,381.00,381.00,379.70,379.70,0
2019-04-02 05:58:00,380.90,380.90,380.85,380.85,5040
2019-04-02 05:57:00,380.85,380.95,380.65,380.95,9615
2019-04-02 05:56:00,380.60,381.20,380.60,381.00,13041
2019-04-02 05:55:00,379.80,380.60,379.80,380.60,19586""")
df = pd.read_csv(csvdata, sep=",", index_col="timestamp", parse_dates=True, infer_datetime_format=True)
print("index type {}".format(type(df.index)))
# is tz 'naive'
print("index tz None is naive {}".format(df.index.tz))
# results
print(df)
# so give it a locale, since input data is naive,
# UTC must be presumed unless there is additional
# input data not specified in above example
df.index = df.index.tz_localize("Etc/UTC")
# is tz 'naive'
print("index tz None is naive {}".format(df.index.tz))
# now that the DateTimeIndex has a tz, it may
# be converted as desired
random_tz = "Asia/Kolkata"
df.index = df.index.tz_convert("Asia/Kolkata")
# results
print(df)
index type <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
index tz None is naive None
open high low close volume
timestamp
2019-04-02 05:59:00 381.00 381.00 379.70 379.70 0
2019-04-02 05:58:00 380.90 380.90 380.85 380.85 5040
2019-04-02 05:57:00 380.85 380.95 380.65 380.95 9615
2019-04-02 05:56:00 380.60 381.20 380.60 381.00 13041
2019-04-02 05:55:00 379.80 380.60 379.80 380.60 19586
index tz None is naive Etc/UTC
open high low close volume
timestamp
2019-04-02 11:29:00+05:30 381.00 381.00 379.70 379.70 0
2019-04-02 11:28:00+05:30 380.90 380.90 380.85 380.85 5040
2019-04-02 11:27:00+05:30 380.85 380.95 380.65 380.95 9615
2019-04-02 11:26:00+05:30 380.60 381.20 380.60 381.00 13041
2019-04-02 11:25:00+05:30 379.80 380.60 379.80 380.60 19586
请接受这是回答问题。