如何从DataFrame的列中提取日期和时间?

时间:2019-10-13 21:30:28

标签: python pandas dataframe

我有一个带有“日期”列的数据框,其值类似于“ 10/9/2019 23:59”。我想创建两列:一列包含日期(“ 10/9/2019”),另一列包含小时(“ 23”)。

这是我的代码:

import pandas as pd
df = pd.read_csv('date.csv', names = ['Date'],
                 low_memory=False, encoding = 'utf-8-sig', header = 0)

cdf = pd.DataFrame(columns =['day' , 'hour'])
i = 0

for index, line in df.iterrows():
    day = datetime.strptime(str(df["Date"]), '%m/%d/%Y %H:%M').date()
    cdf.at[i , 'day'] = day
    hour = datetime.strptime(str(df["Date"]), '%m/%d/%Y %H:%M').hour
    cdf.at[i , 'hour'] = hour  
    i +=1

但我收到此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-26-0550c19671cf> in <module>
      3 
      4 for line in dd:
----> 5     day = datetime.strptime(str(df["Date"]), '%m/%d/%Y %H:%M').date()
      6     cdf.at[i , 'day'] = day
      7     hour = datetime.strptime(str(df["Date"]), '%m/%d/%Y %H:%M').hour

~\AppData\Local\Continuum\anaconda3\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    575     """Return a class cls instance based on the input string and the
    576     format string."""
--> 577     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    578     tzname, gmtoff = tt[-2:]
    579     args = tt[:6] + (fraction,)

~\AppData\Local\Continuum\anaconda3\lib\_strptime.py in _strptime(data_string, format)
    357     if not found:
    358         raise ValueError("time data %r does not match format %r" %
--> 359                          (data_string, format))
    360     if len(data_string) != found.end():
    361         raise ValueError("unconverted data remains: %s" %

ValueError: time data '0        10/9/2019 23:59\n1        10/9/2019 23:59\n2        10/9/2019 23:59\n3        10/9/2019 23:59\n4        10/9/2019 23:59\n5        10/9/2019 23:59\n6        10/9/2019 23:59\n7        10/9/2019 23:59\n8        10/9/2019 23:59\n9        10/9/2019 23:59\n10       10/9/2019 23:59\n11       10/9/2019 23:59\n12       10/9/2019 23:59\n13       10/9/2019 23:59\n14       10/9/2019 23:59\n15       10/9/2019 23:59\n16       10/9/2019 23:58\n17       10/9/2019 23:58\n18       10/9/2019 23:58\n19       10/9/2019 23:58\n20       10/9/2019 23:58\n21       10/9/2019 23:58\n22       10/9/2019 23:58\n23       10/9/2019 23:58\n24       10/9/2019 23:58\n25       10/9/2019 23:58\n26       10/9/2019 23:58\n27       10/9/2019 23:58\n28       10/9/2019 23:58\n29       10/9/2019 23:58\n              ...       \n38584     10/7/2019 2:57\n38585     10/7/2019 2:43\n38586     10/7/2019 2:35\n38587     10/7/2019 2:33\n38588     10/7/2019 2:29\n38589     10/7/2019 2:22\n38590     10/7/2019 2:16\n38591     10/7/2019 2:01\n38592     10/7/2019 1:54\n38593     10/7/2019 1:52\n38594     10/7/2019 1:45\n38595     10/7/2019 1:42\n38596     10/7/2019 1:35\n38597     10/7/2019 1:30\n38598     10/7/2019 1:23\n38599     10/7/2019 1:23\n38600     10/7/2019 1:20\n38601     10/7/2019 1:15\n38602     10/7/2019 1:14\n38603     10/7/2019 1:13\n38604     10/7/2019 1:11\n38605     10/7/2019 1:05\n38606     10/7/2019 0:52\n38607     10/7/2019 0:42\n38608     10/7/2019 0:29\n38609     10/7/2019 0:19\n38610     10/7/2019 0:17\n38611     10/7/2019 0:14\n38612     10/7/2019 0:06\n38613     10/7/2019 0:02\nName: Date, Length: 38614, dtype: object' does not match format '%m/%d/%Y %H:%M'

这里是我数据的a link

2 个答案:

答案 0 :(得分:2)

read_csv()上,指定parse_dates='Date'

这样,您就可以通过执行以下操作获取列:

df['day'] = df['Date'].dt.date
df['hour'] = df['Date'].dt.hour

EDIT1:

意识到您在输入文件上没有标题。因此,您可以通过以下方式将日期列转换为日期时间,而不是在parse_dates上指定read_csv()

df['Date'] = pd.to_datetime(df['Date'])

希望这会有所帮助!

答案 1 :(得分:2)

您可以通过这种方式轻松地做到这一点:

示例框架:

                     Date      vals
0      2000-01-01 00:00:00  0.000000
1      2000-01-01 00:01:00  0.000031
2      2000-01-01 00:02:00  0.000063
3      2000-01-01 00:03:00  0.000094
4      2000-01-01 00:04:00  0.000126

请注意,Date列在datetime64[ns]中。

如果df["Date"] = pd.to_datetime(df["Date"])datesstring或非日期时间的任何dtype,则可以使用object转换日期列

df["day"] = df["Date"].dt.date
df["hour"] = df["Date"].dt.hour