我有一个excel电子表格,列中有一些日期时间数据。我使用pandas将数据导出到一个数据帧中。然而,此列中的日期块将月份和日期交换,而同一列中的其他日期块是正确的。举个例子-
图1:日期和月份被错误地交换
上图显示了交换的日期和月份。日期显示2016-01-10
,但应改为2016-10-01
。将其与同一列中的另一个日期时间值块进行比较-
图2:正确地表示了日和月
在上面的图2中,月份正确地表示为12
,而日期是31
。在
我用了这个问题的答案-How to swap months and days in a datetime object?
我也试过用这个方法- Python Pandas - Day and Month mix up
我还尝试编写自己的函数来映射到条目,但这也没有用-
def dm_swap(day, month):
if(month != 10 or month != 11 or month != 12):
temp = day
day = month
month = temp
t2016Q4.start.map(dmswap, t2016Q4.dt.day, t2016Q4.dt.month)
但是,这两个解决方案都会更改列中的所有日期时间值。因此,当不正确的值得到纠正时,正确的值就变得不正确了。在
为了方便您,我还链接了excel文件。这是一个开放的数据集。在
请选择最后一个数据集Bikeshare Ridership(2016年第4季度)。“开始”和“结束”列有上述问题。在
有没有更有效的方法来清理日期时间数据?在
好吧。在
再次编辑。我运行下面的代码,花了很长时间!我最终流产了,但这在明智的时候也很管用——祝你好运!公司名称:
编辑:我有点挖苦,基于我之前所做的,这看起来是可能的,这里有新代码:
^{pr2}$它有点慢(有一堆数据),但我的计算机似乎正在处理它-如果失败,它会再次更新。在
旧回答: 所以,所发生的是,每个不可能出现歧义的日期/时间都在原始数据集中,格式为:DD/MM/yyyyyhh:MM:SS。在
如果有可能到年月日时:分:秒
我要做的是迭代每个列
Andrew observed可以通过翻转所有月和日来修复数据帧,这样做会产生一个有效的日期。在
这里有一个快速的方法来“翻转”所有的日期。无效的日期被强制转换为NaT(非时间戳)值,然后被删除。剩余的翻转日期可以重新分配给
df
:上面的assert语句验证了结果日期都在2016Q4中,}总是大于其关联的{}。在
trip_start_times
是按时间顺序排列的,并且{您可以在
pd.to_datetime
中使用参数format
:相关问题 更多 >
编程相关推荐