简体   繁体   English

如何使用 Python 将时间拆分为 10 分钟的时间间隔并将其作为单独的列

[英]How to split time into 10 min interval and make it as a separate column using Python

I am working on a data set that has epoch time.我正在研究具有纪元时间的数据集。 I want to create a new column which splits the time into 10 mins time interval.我想创建一个新列,将时间分成 10 分钟的时间间隔。

Suppose timestamp timeblocks 5:00 1 5:02 1 5:11 2假设时间戳时间块 5:00 1 5:02 1 5:11 2

How can i achieve this using python.我如何使用 python 实现这一点。 I tried resampling but i cannot able further process.我尝试重新采样,但无法进一步处理。

I agree with the comments, you need to provide more.我同意评论,你需要提供更多。 After guessing what you're looking for, you may want histograms where intervals are known as bins.在猜出您要查找的内容后,您可能需要将间隔称为 bin 的直方图。 You wrote "10 mins time interval" but your example doesn't show 10 mins.您写了“10 分钟时间间隔”,但您的示例没有显示 10 分钟。

Python's Numpy and matplotlib have histograms for epochs. Python 的Numpy和 matplotlib 具有时代的直方图。

Here's an SO answer on histogram for epoch time.这是关于纪元时间直方图的SO 答案

I'm guessing here, but I believe you are trying to 'bin' your time data into 10 min intervals.我在这里猜测,但我相信您正试图将您的时间数据“装箱”到 10 分钟的时间间隔内。 Epoch or Unix time is represented as time in seconds (or more commonly nowadays, milliseconds). Epoch 或 Unix 时间表示为以秒为单位的时间(或更常见的是现在,毫秒)。

First thing you'll need to do is convert each of your epoch time to minutes.您需要做的第一件事是将每个纪元时间转换为分钟。

Assuming you have a DataFrame and your epoch are in seconds:假设您有一个 DataFrame 并且您的纪元以秒为单位:

df['min] = df['epoch'] // 60

Once that is done, you can bin your data using pd.cut:完成后,您可以使用 pd.cut 合并数据:

df['bins'] = pd.cut(df['min'], bins=pd.interval_range(start=df['min'].min()-1, end=df['min'].max(), freq=10))

Notice that -1 on start is to shift the first bin to beginning of each 10 min interval.请注意,开始时的 -1 是将第一个 bin 移动到每 10 分钟间隔的开始。

You'll have your 'bins', you can rename them to your liking and you can groupby them.您将拥有您的“垃圾箱”,您可以根据自己的喜好重命名它们,并且可以对它们进行groupby

The solution may not be perfect, but it will possibly get you on the right track.解决方案可能并不完美,但它可能会让您走上正轨。

Good luck!祝你好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM