使用Python / Scipy Trapz积分以找到曲线下的面积计算电池安培小时

问题描述

我正在尝试计算系统使用的A.h中的电池使用量。我将数据加载到熊猫中,并带有时间索引和以mA为单位的电池电流。下面的数据框示例:

TIME                    Current
2020-08-07 08:15:54     1.704000
2020-08-07 08:16:04     NaN
2020-08-07 08:16:14     0.852000
2020-08-07 08:16:24     0.852000
2020-08-07 08:16:36     5.965000
                         ...    
2020-08-07 09:14:42    95.446999
2020-08-07 09:14:52    95.446999
2020-08-07 09:15:02    94.595001
2020-08-07 09:15:12    NaN
2020-08-07 09:15:22    98.856003

目前,我正在尝试这样做:

from scipy.integrate import trapz
    def battery_usage(df):
        df = df.dropna(subset=['Current']) # remove row from df when 'Current' is NaN
        amphrs = trapz(df['Current'],x=df['Current'].index,axis=0)
        amphrs = ((amphrs*1E-9)/3600)/1000) #Try to convert mA.ns to A.h
        return amphrs

Trapz函数以纳秒为单位返回timedelta64,我不太确定该怎么做

解决方法

结果实际上似乎是正确的,我假设它知道x轴单位是时间(ns),但是不知道“当前”系列的单位,因此ns x无单位是ns。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...