将 pandas 中的时间列从浮点转换为实际时间值 [英] Convert time column in pandas from float to actual time value

查看:69
本文介绍了将 pandas 中的时间列从浮点转换为实际时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题语句1(容易)
我想将数据框的时间列转换为实际时间值,例如 12:01:45 hh:mm:ss



已尝试过:
df_new [ time_1] = pd.to_datetime(df_new.TIME)这给了我新的一行-但它显示的是日期值而不是时间。 :(



然后我尝试了 df_new ['Time_1'] = pd.to_datetime(df_new ['TIME'],format ='%H% M')。dt.time
但是输出是:
ValueError:时间数据'0'与格式'%H%M'(匹配)不匹配



期望>> time_1 in hh:mm:ss



样本数据:

  PRIMARY_KEY日期时间纬度纬度海拔日期DATE_1时间_1 
0 b04acedfaa0a 43900 0.499964 43.738003 -79.815340 221.0 2020-03-10 1970-01-01
1 efc27d128dac 43900 0.500140 43.736450 -79.813030 221.0 2020-03-10 1970-01-01
2 19b344a8ef88 43900 0.500293 43.730568 -79.821304 216.0 2020-03-10 1970-01-01
3 0c67c3eefb73 43900 0.500431 43.742660 -79.831710 235.0 2020 -03-10 1970-01-01

问题陈述2(复杂性和挑战性)
转换为实际时间后,我想执行一些操作。



输入将是素数ary键值(例如19b344a8ef88)



操作,我要制作满足两个条件的主键对:



a)在50 mtrs的距离附近(通过使用LAT& LONG)和



b)几乎同时说10分钟的时间差



我的输出期望的格式为:

 主键实例
b04acedfaa0a。 5
efc27d128dac。 3

总计5次b04acedfaa0a主键与19b344a8ef88相邻且同时为50 mtrs。 / p>

希望我能自我解释...........

解决方案

第一个问题,快速解决方案是在导入之前在csv中格​​式化数据。在将csv加载到python中之前,您可以尝试在excel中将日期列更改为短日期,将时间列更改为时间并保存csv(将其重命名,以免覆盖原始文件)然后尝试在python中导入?



在python中可能有一种聪明的方法。



对于第二个问题,如果按照以下步骤操作,您可能会得到满意的答案:


  1. 使用 pd.merge()
  2. 使用主键加入数据框
  3. 过滤加入的数据集以满足您使用 df.query()的额外条件(即接近50 mtrs的距离和最大10分钟的时差)

  4. 获取计数每个主键使用 df ['主键'] .value_counts()


获取实例

PROBLEM Statement #1 (EASY) I wanted to convert the time column of my dataframe to actual time value like "12:01:45" hh:mm:ss

Have tried : df_new["time_1"] = pd.to_datetime(df_new.TIME) This has given me a new row - but its showing the date value not the time. :(

then I tried df_new['Time_1'] = pd.to_datetime(df_new['TIME'], format='%H%M').dt.time But output is : ValueError: time data '0' does not match format '%H%M' (match)

expected >> time_1 in hh:mm:ss

sample data :

    PRIMARY_KEY     DATE    TIME        LATITUDE    LONGITUDE   ALTITUDE    DATE_1  time_1
0   b04acedfaa0a    43900   0.499964    43.738003   -79.815340  221.0   2020-03-10  1970-01-01
1   efc27d128dac    43900   0.500140    43.736450   -79.813030  221.0   2020-03-10  1970-01-01
2   19b344a8ef88    43900   0.500293    43.730568   -79.821304  216.0   2020-03-10  1970-01-01
3   0c67c3eefb73    43900   0.500431    43.742660   -79.831710  235.0   2020-03-10  1970-01-01

PROBLEM Statement #2 (COMPLEX & Challenging) After converting to actual time, I want to perform some action.

Input will be a primary key value (say 19b344a8ef88)

Operations, I want to make the pairs of primary keys which satisfies two condition :

a) near 50 mtrs distance (by using LAT & LONG) and

b) At nearly same time, say 10 mins of time difference

Output that I am expecting is in the form of :

Primary Key     Instances
b04acedfaa0a.   5
efc27d128dac.   3

means total 5 times b04acedfaa0a primary key was 50 mtrs near and at the same time with 19b344a8ef88.

Hope I am able to explain myself...........

解决方案

For your first question, quick fix is to format your data in csv before you import. Before you load your csv into python, you could try changing 'DATE' column to 'Short date' and 'TIME' column to 'Time' in excel and save the csv (rename it so that you don't overwrite your original file) and then try importing in python?

Probably there's a clever way to do this in python.

For your second question, you may be able to get what you are after if you follow these steps:

  1. Join dataframes using 'Primary key' using pd.merge()
  2. Filter joined dataset to satisfy your extra conditions using df.query() (i.e. near 50 mtrs distance & max 10 mins of time difference)
  3. Get counts of each primary key to get your instances using df['Primary key'].value_counts()

这篇关于将 pandas 中的时间列从浮点转换为实际时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆