简体   繁体   English

如何在熊猫(Python)中将时间对象转换为日期时间格式?

[英]How to convert time object to datetime format in pandas (python)?

I am new to pandas and practicing some basic functionalities. 我是熊猫的新手,正在练习一些基本功能。 I have a CSV file which contains some data of every minute of some date. 我有一个CSV文件,其中包含某个日期每一分钟的一些数据。 After reading CSV, df.head() gives the following result : 读取CSV后, df.head()给出以下结果:

        Time            C1  C2  C3  C4  C5  C6
0  2016-05-25 03:15:00  0   0   0   0   0   0
1  2016-05-25 03:16:00  0   0   0   0   0   0
2  2016-05-25 03:17:00  0   0   2   0   0   0  
3  2016-05-25 03:18:00  0   0   0   5   0   2
4  2016-05-25 03:19:00  0   0   0   0   0   5

I have used parse_dates option of pd.read_csv . 我已经使用parse_dates的选项pd.read_csv Hence, Time is in datetime64[ns] format. 因此,时间采用datetime64[ns]格式。 Since, the date is the same I don't want to have that on my column. 因为日期是相同的,所以我不想在列上显示该日期。 So, I use 所以,我用

df['Time']=df['Time'].dt.time

It does what I want but it changes the format to object , which I didn't want. 它可以实现我想要的功能,但是它将格式更改为object ,而我却不需要。 Upon suggestions of some other answers, I did the following : 根据其他答案的建议,我做了以下工作:

df['Time']=pd.to_datetime(df['Time'], format="%H:%M:%S")
df['Time'].head()

0      1900-01-01 03:15:00
1      1900-01-01 03:16:00
2      1900-01-01 03:17:00
3      1900-01-01 03:18:00
4      1900-01-01 03:19:00
Name: Time, dtype: datetime64[ns]

This converted the column into datetime64[ns] but added an additional date. 这将列转换为datetime64[ns]但添加了另一个日期。 Is it possible to convert just time into datetime64[ns] ? 是否可以将仅时间转换为datetime64[ns]

No, it is not possible. 不,不可能。 For datetimes always need dates. 对于日期时间,总是需要日期。

But if need working with times, better is use timedelta s by strftime for strings HH:MM:SS with to_timedelta : 但是,如果需要处理时间,最好将strftime timedelta s用于带有to_timedelta字符串HH:MM:SS

df['Time'] = pd.to_timedelta(df['Time'].dt.strftime('%H:%M:%S'))
print (df)
      Time  C1  C2  C3  C4  C5  C6
0 03:15:00   0   0   0   0   0   0
1 03:16:00   0   0   0   0   0   0
2 03:17:00   0   0   2   0   0   0
3 03:18:00   0   0   0   5   0   2
4 03:19:00   0   0   0   0   0   5

print (df.dtypes)
Time    timedelta64[ns]
C1                int64
C2                int64
C3                int64
C4                int64
C5                int64
C6                int64
dtype: object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python熊猫中将时间对象转换为日期时间格式 - Convert time object to datetime format in python pandas 如何在 pandas python 中将字符串转换为日期时间格式? - How to convert string to datetime format in pandas python? 如何将 Python Pandas DateTime 对象转换为字符串 - How to convert a Python Pandas DateTime Object to a string 如何将数据框的日期和时间列转换为熊猫的datetime格式? - how to convert Date and time column of dataframe to datetime format of pandas? Python Pandas日期时间,如何将这些日期转换为Pandas日期时间? - Python pandas date time, how to convert these dates to pandas datetime? 将多个时间格式对象转换为日期时间格式 - Convert multiple time format object as datetime format 如何使用pandas.to_datetime将jalali日期字符串转换为python日期时间对象 - how to convert jalali date string to python date time object with pandas.to_datetime 如何将带有日期、时间和 NaN 的对象列转换为 Python Pandas DataFrame 中的 datetime64 列? - How to convert object column with date, time and NaN to datetime64 column in DataFrame in Python Pandas? 如何在Python Pandas中将带有年份的星期数转换为日期时间格式? - How to convert a week number with year into datetime format in python pandas? 如何将 datetime.time() 对象转换为 datetime.datetime 对象 pandas - how to convert a datetime.time() object to datetime.datetime object pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM