是否有更好的方法根据输入和输出日期时间计算当前库存和平均特征?

2024-05-13 23:43:48 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是我想绘制库存水平。另外,我想要第二行显示某个特征的平均值。对于库存水平,我使用For循环,只需每小时计算一次在该时间进出的数量。这种方法可行,但我无法跟踪目前库存中的对象,因此我无法计算平均库存特征

我已经有了一种解决方法,但我不认为这是一种好的解决方法,也无助于解决第二个问题。我想得到这些物体特征的平均值。例如,重量。那么,在某个时间,库存中某个物体的平均重量是多少呢

所以我所做的是:

import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import datetime
import numpy as np

#datetime of incoming stock. First item is first item in list and will also be the first item in the outgoing list
stock_Incoming = [ '2019-01-01 00:00:00', '2019-01-01 05:00:00', '2019-01-01 06:00:00',  '2019-01-01 06:00:00', '2019-01-01 09:00:00', '2019-01-01 10:00:00', '2019-01-01 10:00:00']
stock_Outgoing = [ '2019-01-01 08:00:00', '2019-01-01 07:00:00', '2019-01-01 09:00:00',  '2019-01-01 09:00:00', '2019-01-01 11:00:00', '2019-01-01 11:00:00', '2019-01-01 11:00:00']
stock_Weight = [2,2,4,4,8,2,1]

stock_panda = pd.DataFrame({ "stock_Incoming": stock_Incoming,"stock_Outgoing": stock_Outgoing , "stock_Weight ": stock_Weight })

stock_panda.stock_Incoming = pd.to_datetime( stock_panda.stock_Incoming, format="%Y-%m-%d %H:%M:%S").apply(lambda x: x.replace(second=0,minute=0))

stock_panda.stock_Outgoing = pd.to_datetime( stock_panda.stock_Outgoing, format="%Y-%m-%d %H:%M:%S").apply(lambda x: x.replace(second=0,minute=0))

stock_panda=stock_panda.dropna().reset_index(drop = True)
inventory=pd.DataFrame(data=None,columns=["number_of_objects"])

Day_Out=pd.to_datetime(stock_panda.stock_Outgoing,format="%d/%m/%Y %H%M%S").apply(lambda x: x.replace(hour=0, minute=0,second=0,microsecond=0))
Day_In=pd.to_datetime(stock_panda.stock_Incoming,format="%d/%m/%Y %H%M%S").apply(lambda x: x.replace(hour=0, minute=0,second=0,microsecond=0))


inventory["Timestamp"]=pd.date_range(stock_panda.stock_Incoming.min(), stock_panda.stock_Outgoing.max(),freq="1H").tolist()


inventory=inventory.set_index("Timestamp")

number_of_objects=0
incoming=0
outgoing=0

for i in pd.date_range(stock_panda.stock_Incoming.min(), stock_panda.stock_Outgoing.max(),freq="1H").tolist():

    incoming=sum(i ==stock_panda.stock_Incoming)
    number_of_objects+=incoming
    outgoing=sum(i ==stock_panda.stock_Outgoing)
    number_of_objects -= outgoing
    inventory.loc[i]=number_of_objects

fig=inventory[["number_of_objects"]].plot(legend=True,ylim=0, figsize=[15,12])

一般来说,我希望有这样一份清单: [2,2,2,2,2,3,3.33,5.33,3.6,3.6]

它应该同时显示库存中所有对象的平均值。我只是不知道怎么去那里。 所以问题是,有没有更好的方法来完成我错过的任务,在哪里可以同时得到库存中的物品

如果能得到任何帮助,我将不胜感激


Tags: of方法importnumberdatetimeobjectsstock库存