使用python将时间戳列拆分为两个独立的日期和时间列

使用python将时间戳列拆分为两个独立的日期和时间列,python,pandas,time,timestamp,Python,Pandas,Time,Timestamp,我有一个名为“df_no_missing”的数据集 我尝试将日期和时间从时间戳列中提取到两个不同的列中,因此: dt = datetime.strptime('TIMESTAMP', '%d/%m/%y %H:%M') df_no_missing['date'] = df_no_missing['TIMESTAMP'].dt.date df_no_missing['time'] = df_no_missing['TIMESTAMP'].dt.time 但我有一个错误: > ValueEr

我有一个名为“df_no_missing”的数据集

我尝试将日期和时间从时间戳列中提取到两个不同的列中,因此:

dt = datetime.strptime('TIMESTAMP', '%d/%m/%y %H:%M')
df_no_missing['date'] = df_no_missing['TIMESTAMP'].dt.date
df_no_missing['time'] = df_no_missing['TIMESTAMP'].dt.time
但我有一个错误:

> ValueError                                Traceback (most recent call
> last) <ipython-input-185-6599284ba17f> in <module>()
>       1 print(df_no_missing.dtypes)
>       2 df_no_missing.head()
> ----> 3 dt = datetime.strptime('TIMESTAMP', '%d/%m/%y %H:%M')
>       4 df_no_missing['date'] = df_no_missing['TIMESTAMP'].dt.date
>       5 df_no_missing['time'] = df_no_missing['TIMESTAMP'].dt.time
> 
> C:\Users\Demonstrator\Anaconda3\lib\_strptime.py in
> _strptime_datetime(cls, data_string, format)
>     508     """Return a class cls instance based on the input string and the
>     509     format string."""
> --> 510     tt, fraction = _strptime(data_string, format)
>     511     tzname, gmtoff = tt[-2:]
>     512     args = tt[:6] + (fraction,)
> 
> C:\Users\Demonstrator\Anaconda3\lib\_strptime.py in
> _strptime(data_string, format)
>     341     if not found:
>     342         raise ValueError("time data %r does not match format %r" %
> --> 343                          (data_string, format))
>     344     if len(data_string) != found.end():
>     345         raise ValueError("unconverted data remains: %s" %
> 
> ValueError: time data 'TIMESTAMP' does not match format '%d/%m/%y
> %H:%M'
有什么办法帮我吗

先谢谢你

您想要的最佳再燃燃料:

df_no_missing['TIMESTAMP'] = pd.to_datetime(df_no_missin['TIMESTAMP'], '%d/%m/%y %H:%M')
然后您可以在转换后执行
.dt.time
dt.date

您还需要发布日期时间字符串的外观

编辑

您可以告诉
read\u csv
在加载时只解析您的日期字符串:

In [42]:
import pandas as pd
import io
t="""TIMESTAMP;P_ACT_KW;PERIODE_TARIF;P_SOUSCR;SITE;TARIF
31/07/2015 23:00;12;HC;;ST GEREON;TURPE_HTA5
31/07/2015 23:10;466;HC;425;ST GEREON;TURPE_HTA5
31/07/2015 23:20;18;HC;425;ST GEREON;TURPE_HTA5
31/07/2015 23:30;17;HC;425;ST GEREON;TURPE_HTA5
31/07/2015 23:40;13;HC;425;ST GEREON;TURPE_HTA5
31/07/2015 23:50;13;HC;425;ST GEREON;TURPE_HTA5
01/08/2015 00:00;13;HC;425;ST GEREON;TURPE_HTA5
01/08/2015 00:10;14;HC;425;ST GEREON;TURPE_HTA5
01/08/2015 00:20;13;HC;425;ST GEREON;TURPE_HTA5
01/08/2015 00:30;20;HC;425;ST GEREON;TURPE_HTA5"""
df = pd.read_csv(io.StringIO(t), sep=';', parse_dates=[0])
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 6 columns):
TIMESTAMP        10 non-null datetime64[ns]
P_ACT_KW         10 non-null int64
PERIODE_TARIF    10 non-null object
P_SOUSCR         9 non-null float64
SITE             10 non-null object
TARIF            10 non-null object
dtypes: datetime64[ns](1), float64(1), int64(1), object(3)
memory usage: 560.0+ bytes
如果您定义

dt = datetime.strptime(TIMESTAMP, '%d/%m/%y %H:%M')
然后
TIMESTAMP
的值必须如下

TIMESTAMP = '03/08/16 16:49'
如果格式定义为

dt = datetime.strptime(TIMESTAMP, '%d/%m/%Y %H:%M')
然后


感谢您的回复,这句话应该是一个可以接受的参数:df_no_missing['TIMESTAMP']=pd.to_datetime(df_no_missing['TIMESTAMP'],'%d/%m/%y%H:%m')。您不认为告诉我问题出在哪里吗?您还可以在问题中发布原始数据和代码以避免歧义它表明:pandas.tslib.array_to_datetime(pandas\tslib.c:38535)中的pandas\tslib.pyx(pandas\tslib.c:38535)()断言者错误:如我所述,除非您发布原始数据和代码,否则我和其他人可能无法帮助您,我不打算进一步研究这个问题,除非你提供原始数据和代码来证明我在我的文章中编辑的错误。问题是,如果有这样的情况,禁止发布数据。你知道这一点;)
df = pd.read_csv(your_file, sep=';', parse_dates=[0])
dt = datetime.strptime(TIMESTAMP, '%d/%m/%y %H:%M')
TIMESTAMP = '03/08/16 16:49'
dt = datetime.strptime(TIMESTAMP, '%d/%m/%Y %H:%M')
TIMESTAMP = '03/08/2016 16:49'