python 3D numpy array time index

时间:2016-08-31 12:26:44

标签: python arrays datetime numpy multidimensional-array

Is there a way to index a 3 dimensional array using some form of time index (datetime etc.) on the 3rd dimension?

My problem is that I am doing time series analysis on several thousand radar images and I need to get, for example, monthly averages. However if i simply average over every 31 arrays in the 3rd dimension it becomes inacurate due to shorter months and missing data etc.

2 个答案:

答案 0 :(得分:1)

你可以使用pandas模块。它支持按日期/日期时间范围编制索引。它还支持多索引,允许您以2D方式处理多维数据。

>>> rng = pd.date_range('1/1/2016', periods=100, freq='D')
>>> rng[:5]

DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03', '2016-01-04', '2016-01-05'], dtype='datetime64[ns]', freq='D')

>>> ts = pd.Series(np.random.randn(len(rng)), index=rng)
>>> ts.head()

2016-01-01    0.119762
2016-01-02   -0.010990
2016-01-03    0.226537
2016-01-04   -0.087559
2016-01-05    0.484426
Freq: D, dtype: float64

>>> ts.resample('M').mean()

2016-01-31   -0.171578
2016-02-29    0.055878
2016-03-31   -0.243225
2016-04-30   -0.015087
Freq: M, dtype: float64

查看以下详细信息:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html http://pandas.pydata.org/pandas-docs/stable/timeseries.html http://pandas.pydata.org/pandas-docs/stable/advanced.html

答案 1 :(得分:0)

发布我使用的伪造解决方案:

这里的问题是缺乏3d阵列数据(即satillite,雷达)的日期时间索引。虽然pandas中有时间序列函数,但是没有数组(据我所知)。

此解决方案是可行的,因为我使用的数据文件在名称中具有日期时间,例如' 200401010000'是' yyyymmddhhMM'。

  1. 构建一个包含所有数据的3d数组(在某些地方缺少时间)。
  2. 使用数据文件列表(os.listdir),创建时间戳列表(长度匹配3d数组长度)
  3. 使用时间戳(2)创建dfa作为dfa索引并创建一个列' inx'运行整数(范围(0,len(数组)=整数= 3d数组的索引)
  4. 使用数据的开始和结束时间以及已知的数据频率(没有丢失的日期时间)创建日期时间索引。使用此作为索引创建新的dfb。
  5. (li)中的
  6. dfb与(3)中的dfa合并。 Aka dfa现在拥有准确的日期时间指数和' inx'包含3d数组索引的列和在缺失数据时使用nan的列。
  7. 使用此功能,您可以将df重新映射到例如1天,取最小值和最大值的' inx'。这为您提供了数组函数的起始位置。

    您还可以在mising日期时插入nans数组(即' inx&min;最大值= nan),以便您的3d数组与实际日期时间的长度相匹配。

    评论您是否有问题,或者您是否知道此问题的更好解决方案/包。