Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert offsets.MonthEnd series to int in Pandas 0.24.2

Tags:

python

pandas

I calculate the delta between two columns of type pd.Period. Before 0.24, it will return a type int, but not any more in 0.24 which will return something like a series of 1 * MonthEnds, 2 * MonthEnds, ... I want to convert this into int type.

I can use apply to make this happen, for example,

df.apply(lambda x: x['z'].n)

or

((df['x'] - df['y']) / np.timedelta64(1, 'M')).round()

But I want to know whether there is another work around.

df = pd.DataFrame({'x':pd.date_range(start='2001-01-01', periods=10), 'y':pd.date_range(start='2002-01-01', periods=10)})

Before Pandas 0.24, the following code will return a column in int type

df['z'] = df['x'].dt.to_period('M') - df['y'].dt.to_period('M')

but 0.24 change the return type, as mentioned above there are two ways to still return an int column, but I would like to know if there are other ways to make this happen.

like image 337
C. Tanaka Avatar asked Oct 28 '25 07:10

C. Tanaka


1 Answers

Using astype will return an int rather than a DateOffset object:

df['x'].dt.to_period('M').astype(int) - df['y'].dt.to_period('M').astype(int)

0   -12
1   -12
2   -12
3   -12
4   -12
5   -12
6   -12
7   -12
8   -12
9   -12
dtype: int64
like image 104
It_is_Chris Avatar answered Oct 31 '25 05:10

It_is_Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!