pandas - 如何矢量化熊猫日期时间列?
问题描述
我正在尝试使用矢量化来获取熊猫数据框列并将特定月份的所有天更改为一天,但是,该列似乎仍被解释为一个系列,而不是被迭代。谁能帮助我做错了什么?
我要更改的列是 ['Date_Time']。例如,如果条目是“2019-05-11 09:00:00”,我想将其更改为“2019-05-15 09:00:00”。
import pandas as pd
from datetime import datetime
from random import random
# Create a mini-dataframe to test on
df = pd.DataFrame(columns=['WindowID', 'Date_Time', 'Occlusion'])
dy = 11
for i in range(6):
x = i+1
hr = 9
while hr < 18:
z = random()
y = datetime(year=2019, month=5, day=dy, hour=hr, minute=00, second=00)
df = df.append({'WindowID': x, 'Date_Time': y, 'Occlusion': z}, ignore_index=True)
hr += 2
dy += 1
# Create a function for normalizing the day of the month
def normmonth(dt):
print(dt.month)
if dt.month == 5:
rpday = 15
elif dt.month == 10:
rpday = 30
dt2 = dt.replace(day=rpday)
return dt2
df['Date_Time2'] = normmonth(df['Date_Time'])
这是错误:“AttributeError:'Series' 对象没有属性'month'”。
我还发现这是一篇关于迭代 pandas 数据帧的非常有用的文章
解决方案
推荐阅读
- tkinter - 何时以及为什么需要将窗口坐标更改为画布坐标?
- vb.net - 试图在函数头中使用类型类型的变量 - 包含类型
- python - 从 Python 与 Node.js 进程通信
- php - php `require()` 一个包含另一个 `require()` 的文件
- python - 在 Heroku 上将自然语言工具包与 Django 结合使用 - - 错误:找不到“nltk.txt”
- r - list.files:正则表达式模式在 Windows 上不起作用?
- migration - 在现有物理主机上无法增加虚拟机容量时,配置虚拟机到另一台物理主机的实时迁移策略
- c++ - 计算复合类的哈希值
- swift - 在使用 Swift 而没有第三方框架的情况下查看生产中抛出的错误的好方法是什么?
- c++ - 正确使用函数的显式模板实例化?