Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python日期时间转换时间格式问题_Python_Pandas_Datetime - Fatal编程技术网

Python日期时间转换时间格式问题

Python日期时间转换时间格式问题,python,pandas,datetime,Python,Pandas,Datetime,我想转换位于panda dataframe列中的以下时间格式 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 我想将以前的时间格式转换为标准的时间格式HH:MM,如下所示 01:00 02:00 03:00 ... 15:00 16:00 ... 22:00 23:00 00:00 我如何用python实现它 提前感谢这

我想转换位于panda dataframe列中的以下时间格式

100
200
300
400
500
600
700
800
900
1000
1100
1200
1300
1400
1500
1600
1700
1800
1900
2000
2100
2200
2300
2400
我想将以前的时间格式转换为标准的时间格式HH:MM,如下所示

01:00
02:00
03:00
...
15:00
16:00
...
22:00
23:00
00:00
我如何用python实现它


提前感谢

这将为您的数据提供一个带有datetime64[ns]和object dtype列的df:

import pandas as pd

df = pd.read_csv('hm.txt', sep=r"[ ]{2,}", engine='python', header=None, names=['pre'])

df['pre_1'] = df['pre'].astype(str).str.replace('00', '')

df['datetime_dtype'] = pd.to_datetime(df['pre_1'], format='%H', exact=False)

df['str_dtype'] = df['datetime_dtype'].astype(str).str[11:16]

print(df.head(5))

    pre datetime_dtype  str_dtype
0   1   1900-01-01 01:00:00 01:00
1   2   1900-01-01 02:00:00 02:00
2   3   1900-01-01 03:00:00 03:00
3   4   1900-01-01 04:00:00 04:00
4   5   1900-01-01 05:00:00 05:00 

print(df.dtypes)

pre                       object
datetime_dtype    datetime64[ns]
str_dtype                 object
dtype: object

这将为您的数据提供一个带有datetime64[ns]和object dtype列的df:

import pandas as pd

df = pd.read_csv('hm.txt', sep=r"[ ]{2,}", engine='python', header=None, names=['pre'])

df['pre_1'] = df['pre'].astype(str).str.replace('00', '')

df['datetime_dtype'] = pd.to_datetime(df['pre_1'], format='%H', exact=False)

df['str_dtype'] = df['datetime_dtype'].astype(str).str[11:16]

print(df.head(5))

    pre datetime_dtype  str_dtype
0   1   1900-01-01 01:00:00 01:00
1   2   1900-01-01 02:00:00 02:00
2   3   1900-01-01 03:00:00 03:00
3   4   1900-01-01 04:00:00 04:00
4   5   1900-01-01 05:00:00 05:00 

print(df.dtypes)

pre                       object
datetime_dtype    datetime64[ns]
str_dtype                 object
dtype: object

这回答了你的问题吗?类似于
df['col2']=pd.to_datetime(df['col1'],格式='%H%M')
感谢您的回复。我不工作,因为棘手的部分是2400小时的格式。我想把格式改成00:00。这能回答你的问题吗?类似于
df['col2']=pd.to_datetime(df['col1'],格式='%H%M')
感谢您的回复。我不工作,因为棘手的部分是2400小时的格式。我想把格式改成00:00。