从excel表导出后如何清理dataframe中的datetime字符串?

2024-09-29 22:03:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个excel电子表格,列中有一些日期时间数据。我使用pandas将数据导出到一个数据帧中。然而,此列中的日期块将月份和日期交换,而同一列中的其他日期块是正确的。举个例子-

enter image description here

图1:日期和月份被错误地交换

上图显示了交换的日期和月份。日期显示2016-01-10,但应改为2016-10-01。将其与同一列中的另一个日期时间值块进行比较-

enter image description here

图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文件。这是一个开放的数据集。在

https://www.toronto.ca/city-government/data-research-maps/open-data/open-data-catalogue/#343faeaa-c920-57d6-6a75-969181b6cbde

请选择最后一个数据集Bikeshare Ridership(2016年第4季度)。“开始”和“结束”列有上述问题。在

有没有更有效的方法来清理日期时间数据?在


Tags: orand数据方法data时间dtopen
3条回答

好吧。在

再次编辑。我运行下面的代码,花了很长时间!我最终流产了,但这在明智的时候也很管用——祝你好运!公司名称:

import pandas as pd

f = "string\to\file\here.xlsx"
df = pd.read_excel(f)

def alter_date(timestamp):

    try:
        date_time = timestamp.to_datetime().strftime("%Y-%d-%m %H:%M:%S")
        time_stamp = pd.Timestamp(date_time)
        return time_stamp
    except:
        return timestamp

new_starts = df["trip_start_time"].apply(alter_date)
df["trip_start_time"] = new_starts
new_ends =  df["trip_stop_time"].apply(alter_date)
df["trip_stop_time"] = new_ends

编辑:我有点挖苦,基于我之前所做的,这看起来是可能的,这里有新代码:

^{pr2}$

它有点慢(有一堆数据),但我的计算机似乎正在处理它-如果失败,它会再次更新。在

旧回答: 所以,所发生的是,每个不可能出现歧义的日期/时间都在原始数据集中,格式为:DD/MM/yyyyyhh:MM:SS。在

如果有可能到年月日时:分:秒

我要做的是迭代每个列

for row in df.index:
    try:
        new_dt = datetime.strptime(row, "%Y-%d-%m %H:%M:%S")
        #write back to the df here
    except ValueError:
        pass#ignore anything  that cannot be converted

Andrew observed可以通过翻转所有月和日来修复数据帧,这样做会产生一个有效的日期。在

这里有一个快速的方法来“翻转”所有的日期。无效的日期被强制转换为NaT(非时间戳)值,然后被删除。剩余的翻转日期可以重新分配给df

import pandas as pd

df = pd.read_excel('2016_Bike_Share_Toronto_Ridership_Q4.xlsx')

for col in ['trip_start_time', 'trip_stop_time']:
    df[col] = pd.to_datetime(df[col])
    swapped = pd.to_datetime({'year':df[col].dt.year, 
                              'month':df[col].dt.day, 
                              'day':df[col].dt.month,
                              'hour':df[col].dt.hour,
                              'minute':df[col].dt.minute,
                              'second':df[col].dt.second,}, errors='coerce')
    swapped = swapped.dropna()
    mask = swapped.index
    df.loc[mask, col] = swapped

# check that now all dates are in 2016Q4
for col in ['trip_start_time', 'trip_stop_time']:
    mask = (pd.PeriodIndex(df[col], freq='Q') == '2016Q4')
    assert mask.all()

# check that `trip_start_times` are in chronological order
assert (df['trip_start_time'].diff().dropna() >= pd.Timedelta(0)).all()

# check that `trip_stop_times` are always greater than `trip_start_times`
assert ((df['trip_stop_time']-df['trip_start_time']).dropna() >= pd.Timedelta(0)).all()

上面的assert语句验证了结果日期都在2016Q4中,trip_start_times是按时间顺序排列的,并且{}总是大于其关联的{}。在

您可以在pd.to_datetime中使用参数format

>>> date= pd.Series(['2016-01-10', '2016-02-10'])
>>> pd.to_datetime(date, format='%Y-%d-%m')
Out: 
0   2016-10-01
1   2016-10-02

相关问题 更多 >

    热门问题