如何选择矩阵的某些元素以使用np.array创建一个新元素?

时间:2019-07-10 20:09:35

标签: python-3.x numpy timedelta

我有一个名为“ times”的矩阵,格式为(1,517),其中一整天的时间是24小时(以大纪元时间为单位),我想用每半小时的时间创建一个新的矩阵,从第一次开始,然后是对应于半小时的那个,依此类推,直到完成一天中所有的半小时,即48

我与 dt = timedelta (hours = 0.5) dts = timedelta.total_seconds (dt)  但我不知道该如何指示我的新矩阵接受这些元素

print(times.shape)
Out[4]: (1, 517)

print(times)
array([[1.55079361e+09, 1.55079377e+09, 1.55079394e+09, 1.55079410e+09,
        1.55079430e+09, 1.55079446e+09, 1.55079462e+09, 1.55079479e+09,
        1.55079495e+09, 1.55079512e+09, 1.55079528e+09, 1.55079544e+09,
        1.55079561e+09, 1.55079577e+09, 1.55079594e+09, 1.55079614e+09,
        1.55079630e+09, 1.55079646e+09, 1.55079663e+09, 1.55079679e+09,
        1.55079695e+09, 1.55079712e+09, 1.55079728e+09, 1.55079744e+09,
        1.55079761e+09, 1.55079781e+09, 1.55079797e+09, 1.55079814e+09,
        1.55079830e+09, 1.55079846e+09, 1.55079863e+09, 1.55079879e+09,
        1.55079895e+09, 1.55079912e+09, 1.55079928e+09, 1.55079945e+09,
        1.55079964e+09, 1.55079981e+09, 1.55079997e+09, 1.55080014e+09,
        1.55080030e+09, 1.55080046e+09, 1.55080063e+09, 1.55080079e+09,
        1.55080096e+09, 1.55080112e+09, 1.55080128e+09, 1.55080148e+09,
        1.55080164e+09, 1.55080181e+09, 1.55080197e+09, 1.55080214e+09,
        1.55080230e+09, 1.55080246e+09, 1.55080263e+09, 1.55080279e+09,
        1.55080296e+09, 1.55080312e+09, 1.55080332e+09, 1.55080348e+09,
        1.55080364e+09, 1.55080381e+09, 1.55080397e+09, 1.55080414e+09,
        1.55080430e+09, 1.55080446e+09, 1.55080463e+09, 1.55080479e+09,
        1.55080496e+09, 1.55080516e+09, 1.55080532e+09, 1.55080548e+09,
        1.55080565e+09, 1.55080581e+09, 1.55080597e+09, 1.55080614e+09,
        1.55080630e+09, 1.55080646e+09, 1.55080663e+09, 1.55080683e+09,
        1.55080699e+09, 1.55080716e+09, 1.55080732e+09, 1.55080748e+09,
        1.55080765e+09, 1.55080781e+09, 1.55080797e+09, 1.55080814e+09,
        1.55080830e+09, 1.55080847e+09, 1.55080866e+09, 1.55080883e+09,
        1.55080899e+09, 1.55080916e+09, 1.55080932e+09, 1.55080948e+09,
        1.55080965e+09, 1.55080981e+09, 1.55080998e+09, 1.55081014e+09,
        1.55081030e+09, 1.55081050e+09, 1.55081066e+09, 1.55081083e+09,
        1.55081099e+09, 1.55081116e+09, 1.55081132e+09, 1.55081148e+09,
        1.55081165e+09, 1.55081181e+09, 1.55081198e+09, 1.55081214e+09,
        1.55081234e+09, 1.55081250e+09, 1.55081266e+09, 1.55081283e+09,
        1.55081299e+09, 1.55081316e+09, 1.55081332e+09, 1.55081348e+09,
        1.55081365e+09, 1.55081381e+09, 1.55081398e+09, 1.55081418e+09,
        1.55081434e+09, 1.55081450e+09, 1.55081467e+09, 1.55081483e+09,
        1.55081499e+09, 1.55081516e+09, 1.55081532e+09, 1.55081548e+09,
        1.55081565e+09, 1.55081585e+09, 1.55081601e+09, 1.55081618e+09,
        1.55081634e+09, 1.55081650e+09, 1.55081667e+09, 1.55081683e+09,
        1.55081699e+09, 1.55081716e+09, 1.55081732e+09, 1.55081749e+09,
        1.55081768e+09, 1.55081785e+09, 1.55081801e+09, 1.55081818e+09,
        1.55081834e+09, 1.55081850e+09, 1.55081867e+09, 1.55081883e+09,
        1.55081900e+09, 1.55081916e+09, 1.55081932e+09, 1.55081952e+09,
        1.55081968e+09, 1.55081985e+09, 1.55082001e+09, 1.55082018e+09,
        1.55082034e+09, 1.55082050e+09, 1.55082067e+09, 1.55082083e+09,
        1.55082100e+09, 1.55082116e+09, 1.55082136e+09, 1.55082152e+09,
        1.55082168e+09, 1.55082185e+09, 1.55082201e+09, 1.55082218e+09,
        1.55082234e+09, 1.55082250e+09, 1.55082267e+09, 1.55082283e+09,
        1.55082300e+09, 1.55082320e+09, 1.55082336e+09, 1.55082352e+09,
        1.55082369e+09, 1.55082385e+09, 1.55082401e+09, 1.55082418e+09,
        1.55082434e+09, 1.55082450e+09, 1.55082467e+09, 1.55082487e+09,
        1.55082503e+09, 1.55082520e+09, 1.55082536e+09, 1.55082552e+09,
        1.55082569e+09, 1.55082585e+09, 1.55082601e+09, 1.55082618e+09,
        1.55082634e+09, 1.55082651e+09, 1.55082670e+09, 1.55082687e+09,
        1.55082703e+09, 1.55082720e+09, 1.55082736e+09, 1.55082752e+09,
        1.55082769e+09, 1.55082785e+09, 1.55082802e+09, 1.55082818e+09,
        1.55082834e+09, 1.55082854e+09, 1.55082870e+09, 1.55082887e+09,
        1.55082903e+09, 1.55082920e+09, 1.55082936e+09, 1.55082952e+09,
        1.55082969e+09, 1.55082985e+09, 1.55083002e+09, 1.55083018e+09,
        1.55083038e+09, 1.55083054e+09, 1.55083070e+09, 1.55083087e+09,
        1.55083103e+09, 1.55083120e+09, 1.55083136e+09, 1.55083152e+09,
        1.55083169e+09, 1.55083185e+09, 1.55083202e+09, 1.55083222e+09,
        1.55083238e+09, 1.55083254e+09, 1.55083271e+09, 1.55083287e+09,
        1.55083303e+09, 1.55083320e+09, 1.55083336e+09, 1.55083352e+09,
        1.55083369e+09, 1.55083389e+09, 1.55083405e+09, 1.55083422e+09,
        1.55083438e+09, 1.55083454e+09, 1.55083471e+09, 1.55083487e+09,
        1.55083503e+09, 1.55083520e+09, 1.55083536e+09, 1.55083553e+09,
        1.55083572e+09, 1.55083589e+09, 1.55083605e+09, 1.55083622e+09,
        1.55083638e+09, 1.55083654e+09, 1.55083671e+09, 1.55083687e+09,
        1.55083704e+09, 1.55083720e+09, 1.55083736e+09, 1.55083756e+09,
        1.55083772e+09, 1.55083789e+09, 1.55083805e+09, 1.55083822e+09,
        1.55083838e+09, 1.55083854e+09, 1.55083871e+09, 1.55083887e+09,
        1.55083904e+09, 1.55083920e+09, 1.55083940e+09, 1.55083956e+09,
        1.55083972e+09, 1.55083989e+09, 1.55084005e+09, 1.55084022e+09,
        1.55084038e+09, 1.55084054e+09, 1.55084071e+09, 1.55084087e+09,
        1.55084104e+09, 1.55084124e+09, 1.55084140e+09, 1.55084156e+09,
        1.55084173e+09, 1.55084189e+09, 1.55084205e+09, 1.55084222e+09,
        1.55084238e+09, 1.55084254e+09, 1.55084271e+09, 1.55084291e+09,
        1.55084307e+09, 1.55084324e+09, 1.55084340e+09, 1.55084356e+09,
        1.55084373e+09, 1.55084389e+09, 1.55084405e+09, 1.55084422e+09,
        1.55084438e+09, 1.55084455e+09, 1.55084474e+09, 1.55084491e+09,
        1.55084507e+09, 1.55084524e+09, 1.55084540e+09, 1.55084556e+09,
        1.55084573e+09, 1.55084589e+09, 1.55084606e+09, 1.55084622e+09,
        1.55084638e+09, 1.55084658e+09, 1.55084674e+09, 1.55084691e+09,
        1.55084707e+09, 1.55084724e+09, 1.55084740e+09, 1.55084756e+09,
        1.55084773e+09, 1.55084789e+09, 1.55084806e+09, 1.55084822e+09,
        1.55084842e+09, 1.55084858e+09, 1.55084874e+09, 1.55084891e+09,
        1.55084907e+09, 1.55084924e+09, 1.55084940e+09, 1.55084956e+09,
        1.55084973e+09, 1.55084989e+09, 1.55085006e+09, 1.55085026e+09,
        1.55085042e+09, 1.55085058e+09, 1.55085075e+09, 1.55085091e+09,
        1.55085107e+09, 1.55085124e+09, 1.55085140e+09, 1.55085156e+09,
        1.55085173e+09, 1.55085193e+09, 1.55085209e+09, 1.55085226e+09,
        1.55085242e+09, 1.55085258e+09, 1.55085275e+09, 1.55085291e+09,
        1.55085307e+09, 1.55085324e+09, 1.55085340e+09, 1.55085357e+09,
        1.55085376e+09, 1.55085393e+09, 1.55085409e+09, 1.55085426e+09,
        1.55085442e+09, 1.55085458e+09, 1.55085475e+09, 1.55085491e+09,
        1.55085508e+09, 1.55085524e+09, 1.55085540e+09, 1.55085560e+09,
        1.55085576e+09, 1.55085593e+09, 1.55085609e+09, 1.55085626e+09,
        1.55085642e+09, 1.55085658e+09, 1.55085675e+09, 1.55085691e+09,
        1.55085708e+09, 1.55085724e+09, 1.55085744e+09, 1.55085760e+09,
        1.55085776e+09, 1.55085793e+09, 1.55085809e+09, 1.55085826e+09,
        1.55085842e+09, 1.55085858e+09, 1.55085875e+09, 1.55085891e+09,
        1.55085908e+09, 1.55085928e+09, 1.55085944e+09, 1.55085960e+09,
        1.55085977e+09, 1.55085993e+09, 1.55086009e+09, 1.55086026e+09,
        1.55086042e+09, 1.55086058e+09, 1.55086075e+09, 1.55086095e+09,
        1.55086111e+09, 1.55086128e+09, 1.55086144e+09, 1.55086160e+09,
        1.55086177e+09, 1.55086193e+09, 1.55086209e+09, 1.55086226e+09,
        1.55086242e+09, 1.55086259e+09, 1.55086278e+09, 1.55086295e+09,
        1.55086311e+09, 1.55086328e+09, 1.55086344e+09, 1.55086360e+09,
        1.55086377e+09, 1.55086393e+09, 1.55086410e+09, 1.55086426e+09,
        1.55086442e+09, 1.55086462e+09, 1.55086478e+09, 1.55086495e+09,
        1.55086511e+09, 1.55086528e+09, 1.55086544e+09, 1.55086560e+09,
        1.55086577e+09, 1.55086593e+09, 1.55086610e+09, 1.55086626e+09,
        1.55086646e+09, 1.55086662e+09, 1.55086678e+09, 1.55086695e+09,
        1.55086711e+09, 1.55086728e+09, 1.55086744e+09, 1.55086760e+09,
        1.55086777e+09, 1.55086793e+09, 1.55086810e+09, 1.55086830e+09,
        1.55086846e+09, 1.55086862e+09, 1.55086879e+09, 1.55086895e+09,
        1.55086911e+09, 1.55086928e+09, 1.55086944e+09, 1.55086960e+09,
        1.55086977e+09, 1.55086997e+09, 1.55087013e+09, 1.55087030e+09,
        1.55087046e+09, 1.55087062e+09, 1.55087079e+09, 1.55087095e+09,
        1.55087111e+09, 1.55087128e+09, 1.55087144e+09, 1.55087161e+09,
        1.55087180e+09, 1.55087197e+09, 1.55087213e+09, 1.55087230e+09,
        1.55087246e+09, 1.55087262e+09, 1.55087279e+09, 1.55087295e+09,
        1.55087312e+09, 1.55087328e+09, 1.55087344e+09, 1.55087364e+09,
        1.55087380e+09, 1.55087397e+09, 1.55087413e+09, 1.55087430e+09,
        1.55087446e+09, 1.55087462e+09, 1.55087479e+09, 1.55087495e+09,
        1.55087512e+09, 1.55087528e+09, 1.55087548e+09, 1.55087564e+09,
        1.55087580e+09, 1.55087597e+09, 1.55087613e+09, 1.55087630e+09,
        1.55087646e+09, 1.55087662e+09, 1.55087679e+09, 1.55087695e+09,
        1.55087712e+09, 1.55087732e+09, 1.55087748e+09, 1.55087764e+09,
        1.55087781e+09, 1.55087797e+09, 1.55087813e+09, 1.55087830e+09,
        1.55087846e+09, 1.55087862e+09, 1.55087879e+09, 1.55087899e+09,
        1.55087915e+09, 1.55087932e+09, 1.55087948e+09, 1.55087964e+09,
        1.55087981e+09]])

1 个答案:

答案 0 :(得分:0)

首先,我们创建一个日期范围在times的第一个条目与最后一个条目之间的数组

t = np.arange(np.datetime64(datetime.datetime.fromtimestamp(times[0,0])), np.datetime64(datetime.datetime.fromtimestamp(times[0,-1])), np.timedelta64(30, 'm'))

t的输出

array(['2019-02-22T01:00:10.000000', '2019-02-22T01:30:10.000000',
       '2019-02-22T02:00:10.000000', '2019-02-22T02:30:10.000000',
       '2019-02-22T03:00:10.000000', '2019-02-22T03:30:10.000000',
       '2019-02-22T04:00:10.000000', '2019-02-22T04:30:10.000000',
       '2019-02-22T05:00:10.000000', '2019-02-22T05:30:10.000000',
       '2019-02-22T06:00:10.000000', '2019-02-22T06:30:10.000000',
       '2019-02-22T07:00:10.000000', '2019-02-22T07:30:10.000000',
       '2019-02-22T08:00:10.000000', '2019-02-22T08:30:10.000000',
       '2019-02-22T09:00:10.000000', '2019-02-22T09:30:10.000000',
       '2019-02-22T10:00:10.000000', '2019-02-22T10:30:10.000000',
       '2019-02-22T11:00:10.000000', '2019-02-22T11:30:10.000000',
       '2019-02-22T12:00:10.000000', '2019-02-22T12:30:10.000000',
       '2019-02-22T13:00:10.000000', '2019-02-22T13:30:10.000000',
       '2019-02-22T14:00:10.000000', '2019-02-22T14:30:10.000000',
       '2019-02-22T15:00:10.000000', '2019-02-22T15:30:10.000000',
       '2019-02-22T16:00:10.000000', '2019-02-22T16:30:10.000000',
       '2019-02-22T17:00:10.000000', '2019-02-22T17:30:10.000000',
       '2019-02-22T18:00:10.000000', '2019-02-22T18:30:10.000000',
       '2019-02-22T19:00:10.000000', '2019-02-22T19:30:10.000000',
       '2019-02-22T20:00:10.000000', '2019-02-22T20:30:10.000000',
       '2019-02-22T21:00:10.000000', '2019-02-22T21:30:10.000000',
       '2019-02-22T22:00:10.000000', '2019-02-22T22:30:10.000000',
       '2019-02-22T23:00:10.000000', '2019-02-22T23:30:10.000000',
       '2019-02-23T00:00:10.000000', '2019-02-23T00:30:10.000000'],
      dtype='datetime64[us]')

现在,我们要将其计算回秒。为此,我们创建一个lambda函数,该函数对数组的单个元素执行此操作,并使用np.apply_along_axis对数组进行逐元素操作。

f = lambda x: (x - np.datetime64('1970-01-01T00:00:00Z'))/np.timedelta64(1,'s')

np.apply_along_axis(f, 0, t)

输出

array([1.55079721e+09, 1.55079901e+09, 1.55080081e+09, 1.55080261e+09,
       1.55080441e+09, 1.55080621e+09, 1.55080801e+09, 1.55080981e+09,
       1.55081161e+09, 1.55081341e+09, 1.55081521e+09, 1.55081701e+09,
       1.55081881e+09, 1.55082061e+09, 1.55082241e+09, 1.55082421e+09,
       1.55082601e+09, 1.55082781e+09, 1.55082961e+09, 1.55083141e+09,
       1.55083321e+09, 1.55083501e+09, 1.55083681e+09, 1.55083861e+09,
       1.55084041e+09, 1.55084221e+09, 1.55084401e+09, 1.55084581e+09,
       1.55084761e+09, 1.55084941e+09, 1.55085121e+09, 1.55085301e+09,
       1.55085481e+09, 1.55085661e+09, 1.55085841e+09, 1.55086021e+09,
       1.55086201e+09, 1.55086381e+09, 1.55086561e+09, 1.55086741e+09,
       1.55086921e+09, 1.55087101e+09, 1.55087281e+09, 1.55087461e+09,
       1.55087641e+09, 1.55087821e+09, 1.55088001e+09, 1.55088181e+09])