相关文章推荐
傻傻的烤面包  ·  sql Post 跟 ...·  1 月前    · 
瘦瘦的木耳  ·  TypeError: ...·  1 月前    · 
坚韧的竹笋  ·  protobuf_MODULE_COMPAT ...·  8 月前    · 
可爱的小蝌蚪  ·  1,win10 ...·  11 月前    · 
谦逊的电梯  ·  MySQL 8.0 ...·  12 月前    · 

datetimeindex object has no attribute apply

在 Pandas 中,时间序列数据通常使用 DatetimeIndex 类型存储。这个错误消息表明,您正在尝试在一个 DatetimeIndex 对象上调用 apply 函数,但 apply 并不是 DatetimeIndex 对象的属性或方法。

如果您想对一个 DatetimeIndex 对象中的每个时间戳执行某种操作,可以使用 map apply 函数。例如:

import pandas as pd
# 创建一个 DatetimeIndex 对象
index = pd.date_range('2022-01-01', '2022-01-03')
# 使用 map 函数将每个时间戳转换为字符串
index_str = index.map(str)
print(index_str)
# 使用 apply 函数将每个时间戳转换为字符串
index_str = index.apply(str)
print(index_str)
Index(['2022-01-01', '2022-01-02', '2022-01-03'], dtype='object')
Index(['2022-01-01', '2022-01-02', '2022-01-03'], dtype='object')

另外,如果您想在 DatetimeIndex 对象上执行更复杂的操作,可以将其转换为普通的 Pandas 索引(例如,使用 reset_index 函数),然后使用 apply 函数。

希望这些信息对您有帮助。

  •