将数据框列从hhmmss转换为hh:mm:ss在Python中。

huangapple go评论67阅读模式
英文:

Converting dataframe column from hhmmss to hh:mm:ss in python

问题

我的时间序列数据框的时间格式是hhmmss,我无法绘制它与我的数据,因为它会跳过,例如,000059 -> 000100在每分钟结束时。 数据头

当我搜索解决方案时,它们都显示将hh:mm:ss转换为秒,但我的时间是hhmmss而不是hh:mm:ss。

我已经尝试将hhmmss转换为字符串,分别使用它们的索引定义hh、mm和ss,将它们转换为整数,然后将hh和mm转换为秒,然后将它们全部加在一起,以获得hhmmss以秒为单位。

data = pd.read_csv("CONT  65.754 248.750 20011120 GEODETIC nT17280", sep=" " )
data['TIME'] = pd.to_datetime(data['TIME'], format='%H%M%S')
data['secs'] = (data.TIME.dt.hour*3600 + data.TIME.dt.minute*60) + data.TIME.dt.second

time_sec = data['secs']
X_Value = data['X']

plt.plot(time_sec, X_Value)

当我尝试这样做时,我收到错误消息:
'time data 4 does not match format '%H%M%S' (match)'

如何将我的时间列从hhmmss格式转换为秒格式?谢谢!1: https://i.stack.imgur.com/jJ9T4.png

英文:

As my time-series dataframe is in the time format hhmmss, I can't plot it against my data without it skipping, for instance, 000059 -> 000100 at the end of every minute. Data Head

When I search for solutions, they all show conversion of hh:mm:ss to seconds, but my time is in hhmmss not hh:mm:ss.

I've tried converting hhmmss to a string, defining the hh, mm and ss separately using their index, converting to an integer and then converting hh and mm to seconds, then adding them all back together to get hhmmss in seconds.

data = pd.read_csv("CONT  65.754 248.750 20011120 GEODETIC nT17280", sep =" " )
data['TIME'] = pd.to_datetime(data['TIME'], format = '%H%M%S')
data['secs'] = (data.TIME.dt.hour*3600 + data.TIME.dt.minute*60) + data.TIME.dt.second

time_sec = data['secs']
X_Value = data['X']

plt.plot(time_sec, X_Value)

When I try this, I get the error:
'time data 4 does not match format '%H%M%S' (match)'

How could I convert my time column in the format hhmmss, to be in the format of seconds?
Many thanks 将数据框列从hhmmss转换为hh:mm:ss在Python中。

答案1

得分: 0

一个pandas Timestamp对象是一个内部的二进制存储格式,不是你所建议的那样。如果你的列TIME是DateTime/TimeStamp类型,那么你可以简单地通过将时间转换为秒来创建一个新列,然后用于绘图:

df['secs'] = (df.TIME.dt.hour*60 + df.TIME.dt.minute)*60 + df.TIME.dt.second
英文:

A pandas Timestamp object is an internal binary storage format not as you suggest. If your column TIME is of type DateTime /TimeStamp then you can simply create a new column by converting the time to seconds and then use that for plotting:

df['secs'] = (df.TIME.dt.hour*60 + df.TIME.dt.minute)*60 + df.TIME.dt.second

答案2

得分: 0

作为替代方法,您可以接受从read_csv读取的整数并直接转换为秒:

data = pd.read_csv("CONT 65.754 248.750 20011120 GEODETIC nT17280", sep=" ")
def func(x):
    s1 = x % 100
    s2 = (x // 100) % 100
    s3 = x // 10000
    return s3 * 3600 + s2 * 60 + s1

df['secs'] = df['TIME'].map(func)

注意:请确保将这段代码嵌入到适当的上下文中,以便它能正常运行。

英文:

As an alternative approach you could accept the integers from read_csv and convert directly to seconds:

data = pd.read_csv("CONT  65.754 248.750 20011120 GEODETIC nT17280", sep =" " )
def func(x):
    s1 = x%100
    s2 = (x//100)%100
    s3 = x//10000
    return s3*3600 + s2*60 + s1


df['secs'] = df['TIME'].map(func)

huangapple
  • 本文由 发表于 2023年2月6日 21:35:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定