python - 如何将 pandas DataFrame 索引从日期时间转换为简单的时间索引
问题描述
我正在尝试评估一些四轴飞行器的飞行数据并获得一些带有纪元时间戳的日志文件。
然后我将它们转换为日期时间值(带有pd.to_datetime([...], unit='ms'))
并截断了一些数字。
我的问题是,我实际上不需要日期,这也使得绘制数据变得更加复杂(不需要的xtick
距离、错误诱导matplotlib.dates locators
等)。
现在我剩下以下索引:
2019-09-13 10:09:16.200,...
2019-09-13 10:09:16.300,...
2019-09-13 10:09:16.400,...
...
2019-09-13 10:12:18.300,...
我的进口:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
import glob
import os.path
from datetime import datetime
from mpl_toolkits.mplot3d import Axes3D
我的数据输入/初始化:
data = pd.read_csv(s,',',index_col=0) # Commands
data2 = pd.read_csv(s2,',',index_col=0) # Observations
d1 = data[data['field.handle']==d].drop(['field.handle','Commanded alpha','Commanded beta','Commanded gamma'], axis=1)
d2 = data2[data2['field.handle']==d].drop(['field.handle','Observed alpha','Observed beta','Observed gamma'], axis=1)
merged_data = pd.concat([d1,d2], axis=1, sort=False)
merged_data.index = truncate_index(merged_data)
filled_merge = merged_data.groupby(merged_data.index).mean().fillna(method='ffill')
finished_merge = filled_merge.copy().dropna()
deviations = finished_merge.copy()
我的绘图代码(有时有效,有时无效 - 取决于数据、定位器和格式化程序)
myFmt = mdates.DateFormatter('%M')
ax = deviations.plot(figsize=(14,9), use_index=True, y=['Positional x deviation','Positional y deviation','Positional z deviation'], subplots=True, sharex=True, layout=(3,1))
for axis in ax:
for axi in axis:
axi.set(xlabel = "Time in minutes (minor ticks in seconds)", ylabel="Deviation in meters")
axi.xaxis.set_major_formatter(myFmt)
axi.xaxis.set_minor_locator(mdates.SecondLocator())
axi.xaxis.set_major_locator(mdates.MinuteLocator())
plt.suptitle(plot_title, fontsize=14)
plt.subplots_adjust(top=0.92)
我认为,如果索引可以以毫秒为单位(或秒的几分之一 - 例如浮点值) - 从第一个日期时间值开始,例如:(
2019-09-13 10:09:16.200
第一个条目)将变为0
或者0.0
,第二个条目将从2019-09-13 10:09:16.300
变为0.1
。遗憾的是,我不能完全删除索引而只用行数来计算,因为我想保留的日期时间中有一些间隔,例如 300 毫秒。
我尝试了不同的方法来一致地绘制我的数据,但最终没有任何效果,我希望使用新索引的新方法能够解决我的问题......
我还查看了pandas
和matplotlib
API(从 timedeltas 到 date2num 等)中可能的候选对象,以实现我设想的索引转换,但没有成功。可能是因为我不太确定这种“转变”的正确术语是什么。
非常感谢任何帮助!
解决方案
如果您的索引如下所示:
>>> d = ['2019-09-13 10:09:16.200',
'2019-09-13 10:09:16.300',
'2019-09-13 10:09:16.400',
'2019-09-13 10:12:18.300']
>>> s = pd.Series([pd.Timestamp(thing) for thing in d])
>>> s
0 2019-09-13 10:09:16.200
1 2019-09-13 10:09:16.300
2 2019-09-13 10:09:16.400
3 2019-09-13 10:12:18.300
dtype: datetime64[ns]
>>>
您可以创建一个 timedelta 系列并获得相对于第一项的总秒数。并使用它。
>>> a = s - s[0]
>>> a
0 00:00:00
1 00:00:00.100000
2 00:00:00.200000
3 00:03:02.100000
dtype: timedelta64[ns]
>>> a.dt.total_seconds()
0 0.0
1 0.1
2 0.2
3 182.1
dtype: float64
>>>
推荐阅读
- jsf - 问题 Primefaces + JSF 这个 XML 文件似乎没有任何与之关联的样式信息
- c# - 一个单元格中有多行的 SQL 子字符串问题
- steam - Steam API/JSON 中的物品买卖订单
- reactjs - Stackblitz 错误:找不到包:firebase
- php - 基于 React 的 WordPress 插件调试基本原理
- python - 带有 numpy 功能的 Numba cc 导出不起作用
- c# - 搜索时找不到图像 [在位图中搜索位图]
- bash - 如何接受完整的命令
- php - 使用 Laravel 和 Vue Js 在输入字段中动态显示获取的数据
- php - Phpmyadmin 页面无法加载