有没有办法将 24 小时时间格式转换为一天的四个类别或四个象限?

Is there a way to convert 24hour time format into four categories or four quadrants of the day?(有没有办法将 24 小时时间格式转换为一天的四个类别或四个象限?)
本文介绍了有没有办法将 24 小时时间格式转换为一天的四个类别或四个象限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试将数据中的时间戳预处理为一天中的四个类别.

I am trying to pre-process timestamp in my data into four categories of the day.

这意味着我需要将对象数据类型转换为类别

This means i need to convert object data type to categories namely

早上 00:00:00 到 11:59:59

Morning for 00:00:00 to 11:59:59

下午 12:00:00 至 15:59:59

Afternoon for 12:00:00 to 15:59:59

晚上 16:00:00 至 19:59:59

Evening for 16:00:00 to 19:59:59

晚上 20:00:00 至 23:59:59

Night for 20:00:00 to 23:59:59

我的时间戳数据看起来像

my timestamp data looks like

transaction timestamp
08:26:00
08:26:00
08:26:00
08:26:00
12:26:00
12:45:00
16:26:00
16:28:00
20:28:00
20:34:00

我希望上述列的输出为

time of day
Morning
Morning
Morning
Morning
Afternoon
Afternoon
Evening
Evening
Night
Night

我应该如何清理这种类型的数据并将其转换为仅 4 个类别?

How shall i clean this type of data and convert it to just 4 categories?

推荐答案

您可以通过 to_timedelta 然后使用 cut:

You can convert values to timedeltas by to_timedelta and then use cut:

df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'])
#if values are python object times convert to strings
#df['transaction timestamp'] = pd.to_timedelta(df['transaction timestamp'].astype(str))

b = pd.to_timedelta(['00:00:00','12:00:00','16:00:00','20:00:00', '24:00:00'])
l = ['Morning','Afternoon','Evening','Night']
df['time of day'] = pd.cut(df['transaction timestamp'], bins=b, labels=l)

print (df)
  transaction timestamp time of day
0              08:26:00     Morning
1              08:26:00     Morning
2              08:26:00     Morning
3              08:26:00     Morning
4              12:26:00   Afternoon
5              12:45:00   Afternoon
6              16:26:00     Evening
7              16:28:00     Evening
8              20:28:00       Night
9              20:34:00       Night

这篇关于有没有办法将 24 小时时间格式转换为一天的四个类别或四个象限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)