Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - operations on groups using transform

Here is my example:

import pandas as pd
import numpy as np

df = pd.DataFrame({'A A': ['one', 'one', 'two', 'two', 'one'] ,
                   'B': ['Ar', 'Br', 'Cr', 'Ar','Ar'] ,
                   'C': ['12/15/2011', '11/11/2001', '08/30/2015', '07/3/1999','03/03/2000' ],
                      'D':[1,7,3,4,5]})

df['C'] = pd.to_datetime(df['C'])

def date_test(x):
    key_date = pd.Timestamp(np.datetime64('2015-08-13'))
    end_date = pd.Timestamp(np.datetime64('2016-10-10'))
    result = False

    for i in x.index:
        if key_date < x[i] < end_date:
            result = True

    return result

def int_test(x):
    result = False
    for i in x.index:
        if 1 < x[i] < 9:
            result = True

    return result

Now I am grouping by column B and transforming column C and D

The following code producess column of ones.

df.groupby(['B'])['D'].transform(int_test)

And the following code produces column of dates

df.groupby(['B'])['C'].transform(date_test)

I would expect them both to produce collection of ones and zeros and not dates. My goal is to get collection of ones and zeros. Any thoughts?

Update: My main goal is to understand how transform works.

like image 668
user1700890 Avatar asked Jul 31 '26 18:07

user1700890


1 Answers

For type consistency with subsequent operations your can do with the results of a transform call, that function tries to cast the resulting Series into the dtype of the selected data it works against. The function source code has this dtype cast explicitly done.

Your boolean data can be turned into dates, thus you obtain a datetime series. Explicitly cast to int to get the expected type:

df.groupby(['B'])['C'].transform(date_test).astype('int64')
like image 176
Zeugma Avatar answered Aug 03 '26 08:08

Zeugma



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!