Python 3.x Python熊猫日期索引系列日期&;将一个时区转换为另一个时区的时间

Python 3.x Python熊猫日期索引系列日期&;将一个时区转换为另一个时区的时间,python-3.x,pandas,datetime,time,Python 3.x,Pandas,Datetime,Time,从外部数据源接收并对齐以下数据 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

从外部数据源接收并对齐以下数据

                        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
**注:**当我调试并查看检索的数据时,它显示为2019-04-02T0205:59:00.0000,但当我打印时,它显示为2019-04-02 05:59:00,为什么? 打印(dfdaily.标题(5))


在上面的代码中,我需要做什么更改来转换时区。对我来说,日期索引不起作用,我不知道为什么。

这里的诀窍是输入数据。根据某些情况,datetime数据将加载为“naive”(即无tz)或“tz-aware”。鉴于上述代码中的MCVE,预期数据加载为“naive”。通过查看最初加载的索引进行检查

df.index.tz

一旦用
DateTimeIndex
构建了数据帧,熊猫就可以完成所需的工作

在下面的回答中,请注意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)

将熊猫作为pd导入
导入系统
如果系统版本信息[0]<3:
从StringIO导入StringIO
其他:
从io导入StringIO
csvdata=StringIO(““”时间戳,打开,高,低,关闭,卷
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\u csv(csvdata,sep=“,”,index\u col=“timestamp”,parse\u dates=True,expert\u datetime\u format=True)
打印(“索引类型{}”。格式(类型(df.index)))
#tz‘天真’吗
打印(“index tz None是朴素的{}”。格式(df.index.tz))
#结果
打印(df)
#所以给它一个区域设置,因为输入数据是幼稚的,
#除非另有规定,否则必须假定UTC
#上述示例中未指定输入数据
df.index=df.index.tz_本地化(“Etc/UTC”)
#tz‘天真’吗
打印(“index tz None是朴素的{}”。格式(df.index.tz))
#现在DateTimeIndex有了tz,它可能
#可根据需要进行转换
random_tz=“亚洲/加尔各答”
df.index=df.index.tz_换算(“亚洲/加尔各答”)
#结果
打印(df)
索引类型
索引tz None是幼稚的None
高开低闭卷
时间戳
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
索引tz None是幼稚的Etc/UTC
高开低闭卷
时间戳
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
请接受这个答案

#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
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