Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建不影响'的时间间隔;不存在于数据帧中_Python_Pandas_Datetime_Time_Intervals - Fatal编程技术网

Python 创建不影响'的时间间隔;不存在于数据帧中

Python 创建不影响'的时间间隔;不存在于数据帧中,python,pandas,datetime,time,intervals,Python,Pandas,Datetime,Time,Intervals,我有-机器错误/机器停止-工厂、工作站、机器、开始日期时间和结束日期时间的详细数据 我想在机器使用python/pandas正常运行时创建时间间隔 因此,我希望有24小时的时间线和每个间隔标记为工作(如果没有发生错误)或不工作 1个站点(共17个)、1个机器类型(共10个)和1天的数据帧如下所示 Stat. Mac. start_date end_date start_no end_no status A B 2019-01-0

我有-机器错误/机器停止-工厂、工作站、机器、开始日期时间和结束日期时间的详细数据

我想在机器使用python/pandas正常运行时创建时间间隔

因此,我希望有24小时的时间线和每个间隔标记为工作(如果没有发生错误)或不工作

1个站点(共17个)、1个机器类型(共10个)和1天的数据帧如下所示

Stat.  Mac.   start_date          end_date            start_no   end_no  status
 A     B    2019-01-03 00:00:00  2019-01-03 01:30:00     1         90     pause
 A     B    2019-01-03 09:35:00  2019-01-03 10:20:00    575        620    pause
 A     B    2019-01-03 20:20:00  2019-01-03 20:40:00    1220       1240   pause
 A     B    2019-01-03 21:45:00  2019-01-03 22:45:00    1305       1365   pause
对于同一工作站机器日对,请求的数据帧应如下所示

    Stat.  Mac.   start_date          end_date            start_no   end_no  status
     A     B    2019-01-03 00:00:00  2019:01:03 00:00:01     0         1      working
     A     B    2019-01-03 00:00:00  2019-01-03 01:30:00     1         90     pause
     A     B    2019-01-03 01:30:00  2019-01-03 09:35:00     90        575    working
     A     B    2019-01-03 09:35:00  2019-01-03 10:20:00    575        620    pause
     A     B    2019-01-03 10:20:00  2019-01-03 20:20:00    620        1220   working
     A     B    2019-01-03 20:20:00  2019-01-03 20:40:00    1220       1240   pause
     A     B    2019-01-03 20:40:00  2019-01-03 21:45:00    1240       1305   working
     A     B    2019-01-03 21:45:00  2019-01-03 22:45:00    1305       1365   pause
     A     B    2019-01-03 22:45:00  2019-01-03 23:59:00    1365       1439   working
我在下面的链接中上传了示例数据帧(1000行-~80kb)

我应该如何处理这个问题


提前感谢

一个快速但缓慢的方法可能是循环所有行并检查当前+下一行。您只有1000行,所以现在就可以了。这将看起来像这样:

import pandas as pd
df = pd.read_excel("sample_2.xlsx")

df['status'] = 'pause'

df = df.sort_values(['Workcenter','Machine','Error_Reason','Class','start_date','start_time', 'end_date','end_time']).reset_index()
new_df = df.copy()

number_rows = len(df)-1
for i in range(number_rows):
    row = df.loc[i]
    next_row = df.loc[i+1]

    new_row = row
    new_row['status'] = 'working'
    new_row['start_date'] = row['end_date']
    new_row['end_date'] = next_row['start_date']
    new_row['start_number'] = row['end_number']
    new_row['end_number'] = next_row['start_number']
    new_df = new_df.append(new_row)

在这个问题中,我们有一个顺序模式,我们可以将“start_no”和“end_no”列转换为所需数据帧的列。 当我们取像
(start\u no0,end\u no0,start\u no1,end\u no1,…)
这样的值时,我们实际上得到了所需的“start\u no”和“end\u no”列的最大部分。通过简单的修复,我们可以得到完全相同的列。同样的逻辑可以应用于开始日期和结束日期,因为它们代表相同的事物

由于您有不同的工作站和机器值,我们可以通过使用Stat.,Mac.,start_date,end_date索引将问题分为多个组。在代码中,我试图通过忽略原始数据集中的时间字段来获取当天的所有值。基本上,我只是将数据分组并迭代每个组,以创建包含所需信息的新数据帧

对于您共享的案例,代码如下所示:

import numpy as np
import pandas as pd

data = pd.read_excel("sample_2.xlsx")

# transform (start|end)_date as only date without time
data["_sDate"] = data.start_date.apply(lambda x: x.strftime("%Y-%m-%d"))
data["_eDate"] = data.end_date.apply(lambda x: x.strftime("%Y-%m-%d"))

# group the data by following columns
grouped = data.groupby(["Station","Machine","_sDate","_eDate"])
# container for storing result of each group
container = []

# iterate the groups
for name, group in grouped:
    # sort them by start_number
    group = group.sort_values("start_number")
    # get (start|end)_numbers into a flatten array
    nums = group[["start_number", "end_number"]].values.flatten()
    # get (start|end)_date into a flatten array
    dates = group[["start_date", "end_date"]].values.flatten()
    ## insert required values to nums and dates
    # we add the first pause time at index 1 to show first working interval
    dates = np.insert(dates, 1 , dates[0] + nums[0]*10**9)
    # we add 0 in the beginning of the array to show first working interval
    nums = np.insert(nums, 0, 0)
    # create df
    nrow = nums.size-1      # decrement, because we add one additional element
    newdf = pd.DataFrame({
        "Station": np.tile(("A"),nrow),
        "Machine": np.tile(("B"),nrow),
        "start_date": dates[:-1],
        "end_date": dates[1:],
        "start_no": nums[:-1],
        "end_no": nums[1:],
        "status": np.tile(["working", "pause"], nrow//2)
    })
    container.append(newdf)

df_final = pd.concat(container)
df_final.index = range(0,df_final.shape[0])

谢谢您的回答,但由于行之间没有if控件,所以它只获取下一行的信息。例如,如果下一排的日期不同,它应该自动完成到1440(24小时*60分钟),但无论如何,再次感谢这是一个很好的开始,这就是为什么您首先需要对它进行排序。我在这里这么做了,但也许你可以用更好的方式分类
df=df.排序_值(['Workcenter'、'Machine'、'Error_Reason'、'Class'、'start_date'、'start_time'、'end_date'、'end_time'])。重置_index()
Iyes。但此方法不会创建0以开始\u编号,并将\u编号结束为1440行。它只在暂停的行之间创建行。例如,一天有一个条目,开始编号为:250结束编号为310。我需要再创建两行。第一行的开始编号:0,结束编号:250-工作,最后一行的开始编号:310和结束编号:1440工作要获得最佳结果,请确保发布的数据和链接文件的格式(列编号和名称)相同。另外,
start\u date
end\u date
在两个数据集中都存在,但含义不同。任何解决方案都必须经过调整才能适用于其他数据集。感谢您的反馈,实际上我只是为了清晰起见发布了重要专栏,但您是对的,我编辑了链接。