Python 熊猫时间序列司

Python 熊猫时间序列司,python,pandas,Python,Pandas,我有一个包含7个特征的表。最后一个是时间戳。我只想把时间序列数据分成10分钟的等分时段。所以我可以检查哪个实例落在哪个插槽中 import pandas as pd import datetime as dt # Create reproduceable example data # (in the future, it's better if you do this in your question) first_timestamp = pd.to_datetime('1/1/2011 0

我有一个包含7个特征的表。最后一个是时间戳。我只想把时间序列数据分成10分钟的等分时段。所以我可以检查哪个实例落在哪个插槽中

import pandas as pd
import datetime as dt

# Create reproduceable example data 
# (in the future, it's better if you do this in your question)
first_timestamp = pd.to_datetime('1/1/2011 00:00')

timestamps = pd.date_range(first_timestamp, periods=100, freq='1Min')
other_data = np.random.randint(0,10,size=(100,))

df = pd.DataFrame({'timestamp': timestamps,
                   'other_data': other_data})

# Create a timedelta of minutes since first timestamp
# timedeltas have attributes for days and seconds, but not minutes.
df['minutes_since_start'] = (df['timestamp'] - first_timestamp).dt.seconds / 60

# Create groups
df['timestamp group'] = pd.cut(df['minutes_since_start'], bins=range(0,101, 10),include_lowest=True)

# first 3 entries
df.head(5)
输出:

   other_data           timestamp  minutes_since_start timestamp group
0           8 2011-01-01 00:00:00                  0.0         [0, 10]
1           5 2011-01-01 00:01:00                  1.0         [0, 10]
2           7 2011-01-01 00:02:00                  2.0         [0, 10]
    other_data           timestamp  minutes_since_start timestamp group
71           1 2011-01-01 01:11:00                 71.0        (70, 80]
72           8 2011-01-01 01:12:00                 72.0        (70, 80]
73           3 2011-01-01 01:13:00                 73.0        (70, 80]
74           0 2011-01-01 01:14:00                 74.0        (70, 80]
75           8 2011-01-01 01:15:00                 75.0        (70, 80]
76           8 2011-01-01 01:16:00                 76.0        (70, 80]
77           0 2011-01-01 01:17:00                 77.0        (70, 80]
78           6 2011-01-01 01:18:00                 78.0        (70, 80]
79           0 2011-01-01 01:19:00                 79.0        (70, 80]
80           5 2011-01-01 01:20:00                 80.0        (70, 80]
从任意时间戳组获取数据,例如启动后70-80分钟

df[df['timestamp group'] == '(70, 80]']
输出:

   other_data           timestamp  minutes_since_start timestamp group
0           8 2011-01-01 00:00:00                  0.0         [0, 10]
1           5 2011-01-01 00:01:00                  1.0         [0, 10]
2           7 2011-01-01 00:02:00                  2.0         [0, 10]
    other_data           timestamp  minutes_since_start timestamp group
71           1 2011-01-01 01:11:00                 71.0        (70, 80]
72           8 2011-01-01 01:12:00                 72.0        (70, 80]
73           3 2011-01-01 01:13:00                 73.0        (70, 80]
74           0 2011-01-01 01:14:00                 74.0        (70, 80]
75           8 2011-01-01 01:15:00                 75.0        (70, 80]
76           8 2011-01-01 01:16:00                 76.0        (70, 80]
77           0 2011-01-01 01:17:00                 77.0        (70, 80]
78           6 2011-01-01 01:18:00                 78.0        (70, 80]
79           0 2011-01-01 01:19:00                 79.0        (70, 80]
80           5 2011-01-01 01:20:00                 80.0        (70, 80]

欢迎来到堆栈溢出!您可以先学习并创建一个示例。这使我们更容易帮助您。谢谢@stephernauch:)
pandas.cut()
可以帮助您,但显然您不能在约会时间直接使用它。但是,如果您创建一列自开始以来的
minutes\u
,那么您可以在自开始以来的
minutes\u
上使用
pandas.cut()
来获得10分钟的分组。下面是一个有人这样做的示例,但是用天代替分钟,您所说的“除以”是什么意思?