I have an operation that works fine, but is generating a:

FutureWarning: TimeSeries broadcasting along DataFrame index by default 
is deprecated.

So I'd like to do the calculation in a way that is coherent with future versions.

I have a pandas.Series and I'd like to divide that Series by each column of a pandas.DataFrame.

So let's say I have and Series, and a DataFrame, with the following shapes:

In [15]: my_series.shape
Out[15]: (1504,)

In [16]: my_df.shape
Out[16]: (1504, 4)

When I run my_series/my_df I get the desired result:

In [17]: (my_series/my_df).shape
Out[17]: (1504, 4)

Of course, implementing the pandas.Series.divide operator comes back with an error:

In [18]: my_series.div(my_df)
ValueError: operands could not be broadcast together with shapes (1504) (1504,4)

I've given the series.apply functionality a try, but haven't gotten what I expect (based on how I use pandas.DataFrame.apply):

In [19]: my_series.apply(pandas.Series.divide, my_df)
Out[19]: ValueError       

In [22]: my_series.apply(pandas.Series.divide, lambda x: my_df[my_df.columns[x]])
Out[22]: TypeError

Any insight, as always, would be greatly appreciated.

有帮助吗?

解决方案

I can't believe I didn't think of doing it this way... quite embarrassing really.

 In [24]: my_df.apply(lambda x: my_series.div(x)).shape
 Out[24]: (1504, 4)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top