Python:在缺少时间戳时将DateTimeIndex一分为二 我有一个DeDeTeMeX索引,如下:您可以看到时间戳是均匀间隔的,除了在从“代码> >‘2005—03-11’15:00 00’< /代码>到 '2005到03-13:17:30:'/COD> 的中间。

Python:在缺少时间戳时将DateTimeIndex一分为二 我有一个DeDeTeMeX索引,如下:您可以看到时间戳是均匀间隔的,除了在从“代码> >‘2005—03-11’15:00 00’< /代码>到 '2005到03-13:17:30:'/COD> 的中间。,python,pandas,numpy,Python,Pandas,Numpy,如何通过编程在缺少时间戳的点拆分DateTimeIndex并返回2个DateTimeIndex DateTimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00', '2005-03-11 12:00:00', '2005-03-11 12:30:00', '2005-03-11 13:00:00', '2005-03-11 13:30:00', '200

如何通过编程在缺少时间戳的点拆分DateTimeIndex并返回2个DateTimeIndex

DateTimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00',
               '2005-03-11 12:00:00', '2005-03-11 12:30:00',
               '2005-03-11 13:00:00', '2005-03-11 13:30:00',
               '2005-03-11 14:00:00', '2005-03-11 14:30:00',
               '2005-03-11 15:00:00', '2005-03-13 17:00:00',
               '2005-03-13 17:30:00', '2005-03-13 18:00:00',
               '2005-03-13 18:30:00', '2005-03-13 19:00:00',
               '2005-03-13 19:30:00', '2005-03-13 20:00:00',
               '2005-03-13 20:30:00', '2005-03-13 21:00:00',
               '2005-03-13 21:30:00', '2005-03-13 22:00:00',
               '2005-03-13 22:30:00', '2005-03-13 23:00:00',
               '2005-03-13 23:30:00', '2005-03-14 00:00:00')]
尝试上面的代码,希望对您有所帮助


请尝试上面的代码,希望它对您有所帮助。您可以使用
diff
查找序列中缺少的元素,然后使用
numpy.split
在缺少的元素处进行拆分:

# get the time difference between each timestamp
time_diffs = data.to_series().diff()

# split at each break in the time data
new_data = np.split(data, np.where(time_diffs > np.median(time_diffs)))
编辑:使用显式循环代替
numpy.split
并使用
numpy.diff
代替
pandas.series.diff
的早期答案:

time_diffs = np.diff(data)
new_data = []
start_idx = 0

# loop once for each break in the data
for idx in np.where(time_diffs > np.median(time_diffs)):

    # build a new piece at each break in the data
    new_data.append(data[start_idx:idx+1])
    start_idx = idx+1

# add the last piece to the list
new_data.append(data[start_idx:])
可以使用此作为数据运行上述操作:

import numpy as np
import pandas as pd

data = pd.DatetimeIndex([
    '2005-03-11 11:00:00', '2005-03-11 11:30:00',
    '2005-03-11 12:00:00', '2005-03-11 12:30:00',
    '2005-03-11 13:00:00', '2005-03-11 13:30:00',
    '2005-03-11 14:00:00', '2005-03-11 14:30:00',
    '2005-03-11 15:00:00', '2005-03-13 17:00:00',
    '2005-03-13 17:30:00', '2005-03-13 18:00:00',
    '2005-03-13 18:30:00', '2005-03-13 19:00:00',
    '2005-03-13 19:30:00', '2005-03-13 20:00:00',
    '2005-03-13 20:30:00', '2005-03-13 21:00:00',
    '2005-03-13 21:30:00', '2005-03-13 22:00:00',
    '2005-03-13 22:30:00', '2005-03-13 23:00:00',
    '2005-03-13 23:30:00', '2005-03-14 00:00:00'
])

您可以使用
diff
查找序列中缺少的元素,然后使用
numpy.split
在缺少的元素处进行拆分:

# get the time difference between each timestamp
time_diffs = data.to_series().diff()

# split at each break in the time data
new_data = np.split(data, np.where(time_diffs > np.median(time_diffs)))
编辑:使用显式循环代替
numpy.split
并使用
numpy.diff
代替
pandas.series.diff
的早期答案:

time_diffs = np.diff(data)
new_data = []
start_idx = 0

# loop once for each break in the data
for idx in np.where(time_diffs > np.median(time_diffs)):

    # build a new piece at each break in the data
    new_data.append(data[start_idx:idx+1])
    start_idx = idx+1

# add the last piece to the list
new_data.append(data[start_idx:])
可以使用此作为数据运行上述操作:

import numpy as np
import pandas as pd

data = pd.DatetimeIndex([
    '2005-03-11 11:00:00', '2005-03-11 11:30:00',
    '2005-03-11 12:00:00', '2005-03-11 12:30:00',
    '2005-03-11 13:00:00', '2005-03-11 13:30:00',
    '2005-03-11 14:00:00', '2005-03-11 14:30:00',
    '2005-03-11 15:00:00', '2005-03-13 17:00:00',
    '2005-03-13 17:30:00', '2005-03-13 18:00:00',
    '2005-03-13 18:30:00', '2005-03-13 19:00:00',
    '2005-03-13 19:30:00', '2005-03-13 20:00:00',
    '2005-03-13 20:30:00', '2005-03-13 21:00:00',
    '2005-03-13 21:30:00', '2005-03-13 22:00:00',
    '2005-03-13 22:30:00', '2005-03-13 23:00:00',
    '2005-03-13 23:30:00', '2005-03-14 00:00:00'
])

这应该行得通。您也有一些语法错误

times = pd.DatetimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00',
           '2005-03-11 12:00:00', '2005-03-11 12:30:00',
           '2005-03-11 13:00:00', '2005-03-11 13:30:00',
           '2005-03-11 14:00:00', '2005-03-11 14:30:00',
           '2005-03-11 15:00:00', '2005-03-13 17:00:00',
           '2005-03-13 17:30:00', '2005-03-13 18:00:00',
           '2005-03-13 18:30:00', '2005-03-13 19:00:00',
           '2005-03-13 19:30:00', '2005-03-13 20:00:00',
           '2005-03-13 20:30:00', '2005-03-13 21:00:00',
           '2005-03-13 21:30:00', '2005-03-13 22:00:00',
           '2005-03-13 22:30:00', '2005-03-13 23:00:00',
           '2005-03-13 23:30:00', '2005-03-14 00:00:00'])

early = pd.DatetimeIndex(times[:9])
late = pd.DatetimeIndex(times[9:])
如果尝试拆分数据帧,请尝试:

time_split = '2005-03-11 15:00:00'
early = df.ix[:time_split].index
late = df.ix[time_split:].index

这应该行得通。您也有一些语法错误

times = pd.DatetimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00',
           '2005-03-11 12:00:00', '2005-03-11 12:30:00',
           '2005-03-11 13:00:00', '2005-03-11 13:30:00',
           '2005-03-11 14:00:00', '2005-03-11 14:30:00',
           '2005-03-11 15:00:00', '2005-03-13 17:00:00',
           '2005-03-13 17:30:00', '2005-03-13 18:00:00',
           '2005-03-13 18:30:00', '2005-03-13 19:00:00',
           '2005-03-13 19:30:00', '2005-03-13 20:00:00',
           '2005-03-13 20:30:00', '2005-03-13 21:00:00',
           '2005-03-13 21:30:00', '2005-03-13 22:00:00',
           '2005-03-13 22:30:00', '2005-03-13 23:00:00',
           '2005-03-13 23:30:00', '2005-03-14 00:00:00'])

early = pd.DatetimeIndex(times[:9])
late = pd.DatetimeIndex(times[9:])
如果尝试拆分数据帧,请尝试:

time_split = '2005-03-11 15:00:00'
early = df.ix[:time_split].index
late = df.ix[time_split:].index

我假设在我们分裂之前,分歧是一致的

split = tidx.to_series().diff().diff().abs().idxmax()

t1 = tidx[tidx < split]
t2 = tidx[tidx >= split]

print(split)

2005-03-13 17:00:00

print(t1)

DatetimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00',
               '2005-03-11 12:00:00', '2005-03-11 12:30:00',
               '2005-03-11 13:00:00', '2005-03-11 13:30:00',
               '2005-03-11 14:00:00', '2005-03-11 14:30:00',
               '2005-03-11 15:00:00'],
              dtype='datetime64[ns]', freq=None)

print(t2)

DatetimeIndex(['2005-03-13 17:00:00', '2005-03-13 17:30:00',
               '2005-03-13 18:00:00', '2005-03-13 18:30:00',
               '2005-03-13 19:00:00', '2005-03-13 19:30:00',
               '2005-03-13 20:00:00', '2005-03-13 20:30:00',
               '2005-03-13 21:00:00', '2005-03-13 21:30:00',
               '2005-03-13 22:00:00', '2005-03-13 22:30:00',
               '2005-03-13 23:00:00', '2005-03-13 23:30:00',
               '2005-03-14 00:00:00'],
              dtype='datetime64[ns]', freq=None)
split=tidx.to_series().diff().diff().abs().idxmax()
t1=tidx[tidx=split]
打印(拆分)
2005-03-13 17:00:00
打印(t1)
DatetimeIndex(['2005-03-11 11:00:00','2005-03-11 11:30:00',
'2005-03-11 12:00:00', '2005-03-11 12:30:00',
'2005-03-11 13:00:00', '2005-03-11 13:30:00',
'2005-03-11 14:00:00', '2005-03-11 14:30:00',
'2005-03-11 15:00:00'],
dtype='datetime64[ns]',freq=None)
打印(t2)
DatetimeIndex(['2005-03-13 17:00:00','2005-03-13 17:30:00',
'2005-03-13 18:00:00', '2005-03-13 18:30:00',
'2005-03-13 19:00:00', '2005-03-13 19:30:00',
'2005-03-13 20:00:00', '2005-03-13 20:30:00',
'2005-03-13 21:00:00', '2005-03-13 21:30:00',
'2005-03-13 22:00:00', '2005-03-13 22:30:00',
'2005-03-13 23:00:00', '2005-03-13 23:30:00',
'2005-03-14 00:00:00'],
dtype='datetime64[ns]',freq=None)

我假设在我们分开之前,差异是一致的

split = tidx.to_series().diff().diff().abs().idxmax()

t1 = tidx[tidx < split]
t2 = tidx[tidx >= split]

print(split)

2005-03-13 17:00:00

print(t1)

DatetimeIndex(['2005-03-11 11:00:00', '2005-03-11 11:30:00',
               '2005-03-11 12:00:00', '2005-03-11 12:30:00',
               '2005-03-11 13:00:00', '2005-03-11 13:30:00',
               '2005-03-11 14:00:00', '2005-03-11 14:30:00',
               '2005-03-11 15:00:00'],
              dtype='datetime64[ns]', freq=None)

print(t2)

DatetimeIndex(['2005-03-13 17:00:00', '2005-03-13 17:30:00',
               '2005-03-13 18:00:00', '2005-03-13 18:30:00',
               '2005-03-13 19:00:00', '2005-03-13 19:30:00',
               '2005-03-13 20:00:00', '2005-03-13 20:30:00',
               '2005-03-13 21:00:00', '2005-03-13 21:30:00',
               '2005-03-13 22:00:00', '2005-03-13 22:30:00',
               '2005-03-13 23:00:00', '2005-03-13 23:30:00',
               '2005-03-14 00:00:00'],
              dtype='datetime64[ns]', freq=None)
split=tidx.to_series().diff().diff().abs().idxmax()
t1=tidx[tidx=split]
打印(拆分)
2005-03-13 17:00:00
打印(t1)
DatetimeIndex(['2005-03-11 11:00:00','2005-03-11 11:30:00',
'2005-03-11 12:00:00', '2005-03-11 12:30:00',
'2005-03-11 13:00:00', '2005-03-11 13:30:00',
'2005-03-11 14:00:00', '2005-03-11 14:30:00',
'2005-03-11 15:00:00'],
dtype='datetime64[ns]',freq=None)
打印(t2)
DatetimeIndex(['2005-03-13 17:00:00','2005-03-13 17:30:00',
'2005-03-13 18:00:00', '2005-03-13 18:30:00',
'2005-03-13 19:00:00', '2005-03-13 19:30:00',
'2005-03-13 20:00:00', '2005-03-13 20:30:00',
'2005-03-13 21:00:00', '2005-03-13 21:30:00',
'2005-03-13 22:00:00', '2005-03-13 22:30:00',
'2005-03-13 23:00:00', '2005-03-13 23:30:00',
'2005-03-14 00:00:00'],
dtype='datetime64[ns]',freq=None)

我喜欢这个解决方案,这就是我想要的。但是,当尝试使用numpy.ndarray类型的切片条目时,我收到错误
无法将其强制为integer
。你知道那可能是什么吗?我发现了:我必须把np中idx的
行改成np中idx的
行,其中(time\u diffs>np.median(time\u diffs))
改成
,其中(time\u diffs>np.median(time\u diffs))[0]
。非常感谢,非常有用!
np.where(time_diff>np.median(time_diff))
where在元组中出于某种原因,我喜欢这个解决方案,这就是我想要的。但是,当尝试使用numpy.ndarray类型的切片条目时,我收到错误
无法将其强制为integer
。你知道那可能是什么吗?我发现了:我必须把np中idx的
行改成np中idx的
行,其中(time\u diffs>np.median(time\u diffs))
改成
,其中(time\u diffs>np.median(time\u diffs))[0]
。非常感谢,非常有用!
np.where(time_diff>np.median(time_diff))
where在元组中的结果出于某些原因这是好的,但是我正在寻找一种解决方案,如果存在超过1个间隙,则可以将数据拆分为2个以上的图片。我认为下面的斯蒂芬·劳赫(Stephen Rauch)更接近这一点。但是谢谢!这很好,但是我正在寻找一种解决方案,如果存在超过1个间隙,可以将数据拆分为2个以上的图片。我认为下面的斯蒂芬·劳赫(Stephen Rauch)更接近这一点。但是谢谢!