简体   繁体   English

numpy数组元素乘以熊猫时间序列

[英]numpy array elementwise multiply a panda timeseries

I have these two data structures: 我有这两种数据结构:

a = np.array([1,2,3])
ts = pd.TimeSeries([1,2,3])

What I want to get at the end is: 我最终想要得到的是:

1  2  3
2  4  6
3  6  9

You can use the outer product: 您可以使用外部产品:

In [490]: np.outer(a, ts)
Out[490]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

Or align one of them vertically first: 或者首先垂直对齐其中一个:

In [491]: a * ts[:, None]
Out[491]: 
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

Note that the strange index just makes it a column vector: 请注意,奇怪的索引只是使它成为列向量:

In [493]: ts[:, None]
Out[493]: 
array([[1],
       [2],
       [3]])

by adding an extra length-1 dimension to the shape: 通过在形状中添加额外的长度为1的尺寸:

In [494]: ts[:, None].shape
Out[494]: (3, 1)
>>> np.outer(a, ts)
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM