Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Multidimensional Array_Pandas - Fatal编程技术网

Python 熊猫的时间序列简介?

Python 熊猫的时间序列简介?,python,arrays,multidimensional-array,pandas,Python,Arrays,Multidimensional Array,Pandas,不久前我发现了熊猫,它看起来很有趣。到目前为止,我通常在Python中使用结构化掩码数组,熊猫似乎可以提供一种替代方法,特别是因为我经常使用时间序列。但是,在每个时刻,我都有一些标量和一些数组数据:标量时间、标量纬度、标量经度,以及矢量压力和矢量剖面: dtype([('lat','我将您的数据展平,并将其放入数据框中: import numpy as np import datetime import pandas x = np.array([ (70.00604248046875

不久前我发现了熊猫,它看起来很有趣。到目前为止,我通常在Python中使用结构化掩码数组,熊猫似乎可以提供一种替代方法,特别是因为我经常使用时间序列。但是,在每个时刻,我都有一些标量和一些数组数据:标量时间、标量纬度、标量经度,以及矢量压力和矢量剖面:


dtype([('lat','我将您的数据展平,并将其放入数据框中:

import numpy as np
import datetime
import pandas

x = np.array([ 
    (70.00604248046875, [1.8543829917907715, 1.8604300022125244, 1.8616620302200317, 1.8601950407028198, 1.851915955543518, 1.8489810228347778, 1.8222110271453857, 1.8655049800872803, 1.7066600322723389, 1.687608003616333, 1.2694480419158936, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], datetime.datetime(2010, 3, 23, 2, 24, 54, 210000), -52.05939865112305, [863.52294921875, 669.9515380859375, 583.3341064453125, 507.91802978515625, 442.25689697265625, 385.1185607910156, 335.3567199707031, 292.0868225097656, 246.5142822265625, 183.48988342285156, 72.71968078613281, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0]),
    (83.92768859863281, [1.8359440565109253, 1.8322449922561646, 1.834841012954712, 1.824031949043274, 1.8172659873962402, 1.8016170263290405, 1.8238199949264526, 1.6459150314331055, 1.6390000581741333, 1.2601679563522339, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0], datetime.datetime(2010, 3, 30, 21, 3, 12, 478000), -66.69522094726562, [850.73779296875, 612.4067993164062, 513.5870361328125, 447.0830993652344, 389.2227478027344, 338.8443298339844, 295.01129150390625, 248.8583984375, 190.45912170410156, 78.76023864746094, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0, -9999.0])
    ], 
    dtype=[('lat', '<f4'), ('ch4', '<f4', (110,)), ('time', '<M8[us]'), ('lon', '<f4'), ('p', '<f4', (110,))]
)

finallist = []
for n, row in enumerate(x):
    for m, (p, ch4) in enumerate(zip(row['p'], row['ch4'])): 
        outputrow = [n, m]
        for ic in ['lat', 'lon', 'time']:
            outputrow.append(row[ic])
        outputrow.extend([p, ch4])
        finallist.append(outputrow)

df = pandas.DataFrame(finallist, columns=['series', 'measurement', 'lat', 'lon', 'time', 'p', 'ch4'])
#df.set_index(['series', 'measurement'], inplace=True)
df['p'][df['p'] < 0] = np.nan
df['ch4'][df['ch4'] < 0] = np.nan

print(df.head().to_string())  
以及:

那么:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df.plot(x='ch4', y='p',  use_index=True, subplots=True, ax=ax, legend=True)
ax.set_ylabel(r'$p$')
ax.set_xlabel(r'$\mathrm{CH}_4$')
给出:

或者,如果您可以安装seaborn(强烈建议):

给予


似乎
面板
可以工作,或者
数据框上的
多索引
可以工作。要获得更具体的示例,请发布一些数据。@PaulH我添加了一个包含两个条目的示例。哦,天哪。这有可能来自csv或文本文件吗?@PaulH它来自HDF5文件。pandas可以直接读取HDF5文件。请查看我的回复通过你的数组,我看到了,这里的问题是,在重复输入时间的情况下,轮廓都沿着相同的轴。因此,例如,要获得每个轮廓的第一级,我需要使用一个等于我的压力维度的步长进行索引?@gerrit我有一个不同的想法,坐下tight@gerrit看看这个
df.head()

   series  measurement        lat        lon                       time           p       ch4
0       0            0  70.006042 -52.059399 2010-03-23 02:24:54.210000  863.522949  1.854383
1       0            1  70.006042 -52.059399 2010-03-23 02:24:54.210000  669.951538  1.860430
2       0            2  70.006042 -52.059399 2010-03-23 02:24:54.210000  583.334106  1.861662
3       0            3  70.006042 -52.059399 2010-03-23 02:24:54.210000  507.918030  1.860195
4       0            4  70.006042 -52.059399 2010-03-23 02:24:54.210000  442.256897  1.851916
df.tail()
     series  measurement        lat        lon                       time   p  ch4
215       1          105  83.927689 -66.695221 2010-03-30 21:03:12.478000 NaN  NaN
216       1          106  83.927689 -66.695221 2010-03-30 21:03:12.478000 NaN  NaN
217       1          107  83.927689 -66.695221 2010-03-30 21:03:12.478000 NaN  NaN
218       1          108  83.927689 -66.695221 2010-03-30 21:03:12.478000 NaN  NaN
219       1          109  83.927689 -66.695221 2010-03-30 21:03:12.478000 NaN  NaN
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df.plot(x='ch4', y='p',  use_index=True, subplots=True, ax=ax, legend=True)
ax.set_ylabel(r'$p$')
ax.set_xlabel(r'$\mathrm{CH}_4$')
seaborn.lmplot('ch4', 'p', data=df, hue='series', fit_reg=False)