Python 如何通过将时间戳指定为键值从字典中获取值。?
我通过迭代Series对象来创建字典。这个系列看起来像这样Python 如何通过将时间戳指定为键值从字典中获取值。?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我通过迭代Series对象来创建字典。这个系列看起来像这样 2018-02-05 00:00:00+00:00 4803.0 2018-02-05 00:15:00+00:00 4803.0 2018-02-05 00:30:00+00:00 4806.0 2018-02-05 00:45:00+00:00 4806.0 Freq: 15T, Name: Biomass, dtype: float64 通过提供索引作为键和数据作为值来创建字典,字典如下所示 print(
2018-02-05 00:00:00+00:00 4803.0
2018-02-05 00:15:00+00:00 4803.0
2018-02-05 00:30:00+00:00 4806.0
2018-02-05 00:45:00+00:00 4806.0
Freq: 15T, Name: Biomass, dtype: float64
通过提供索引作为键和数据作为值来创建字典,字典如下所示
print(Entsoe.dictionary_value)
{Timestamp('2018-02-05 00:00:00+0000', tz='UTC', freq='15T'): 4803.0,
Timestamp('2018-02-05 00:15:00+0000', tz='UTC', freq='15T'): 4803.0,
Timestamp('2018-02-05 00:30:00+0000', tz='UTC', freq='15T'): 4806.0,
Timestamp('2018-02-05 00:45:00+0000', tz='UTC', freq='15T'): 4806.0}
Dictionary是类成员,因此使用类名“Entsoe”进行访问
使用此代码分别打印密钥和值对
for key in Entsoe.dictionary_value:
print(f"key: {key}, value: {Entsoe.dictionary_value[key]}")
输出结果如下:
key: 2018-02-05 00:00:00+00:00, value: 4803.0
key: 2018-02-05 00:15:00+00:00, value: 4803.0
key: 2018-02-05 00:30:00+00:00, value: 4806.0
key: 2018-02-05 00:45:00+00:00, value: 4806.0
但是当尝试使用像这样的键值访问时
“没有”
有人能给我解释一下这里出了什么问题吗
Entsoe
字典中的键当前是Timestamp
对象,而不仅仅是表示特定时间戳的字符串。尝试打印(Entsoe.dictionary\u value.get(Timestamp('2018-02-05 00:00:00+0000',tz='UTC',freq='15T'))@Chillie所以我现在需要将密钥转换为Timestamp
对象。您有了s.to_dict()
@Chillie尝试了您的解决方案,显示未解决的时间戳引用错误
@Wen这里是s
,你能详细说明你的答案吗。
print(Entsoe.dictionary_value.get('2018-02-05 00:00:00+00:00'))