Skip to content

Left merging two dataframes lead to an empty Dataframe as a result #3333

Closed

Description

@avlahop

I have the following code (MCVE):

raw_data = pd.DataFrame({'username':list('ab')*10, 'user_agent': list('cdef')*5, 'method':['POST'] * 20, 'dst_port':[80]*20, 'dst':['1.1.1.1']*20})
past = pd.DataFrame({'user_agent':list('cde'), 'percent':[0.3, 0.3, 0.4]})
past = past.set_index('user_agent')
import dask.dataframe as dd
dask_raw = dd.from_pandas(raw_data, npartitions=4)
dask_past = dd.from_pandas(past, npartitions=4)
merged_raw = dask_raw.merge(dask_past, how='left', left_on='user_agent', right_index=True)

So merged_raw is a dask dataframe that should contain NaN values in percent column. In deed calling compute on merged_raw

Out[20]: 
    dst  dst_port method user_agent username  percent
12  1.1.1.1        80   POST          c        a      0.3
16  1.1.1.1        80   POST          c        a      0.3
8   1.1.1.1        80   POST          c        a      0.3
0   1.1.1.1        80   POST          c        a      0.3
4   1.1.1.1        80   POST          c        a      0.3
10  1.1.1.1        80   POST          e        a      0.4
11  1.1.1.1        80   POST          f        b      NaN
13  1.1.1.1        80   POST          d        b      0.3
14  1.1.1.1        80   POST          e        a      0.4
15  1.1.1.1        80   POST          f        b      NaN
17  1.1.1.1        80   POST          d        b      0.3
18  1.1.1.1        80   POST          e        a      0.4
19  1.1.1.1        80   POST          f        b      NaN
5   1.1.1.1        80   POST          d        b      0.3
6   1.1.1.1        80   POST          e        a      0.4
7   1.1.1.1        80   POST          f        b      NaN
9   1.1.1.1        80   POST          d        b      0.3
1   1.1.1.1        80   POST          d        b      0.3
2   1.1.1.1        80   POST          e        a      0.4
3   1.1.1.1        80   POST          f        b      NaN

Moving with the following computations, I get an empty DataFrame:

grouped_by_df = merged_raw.groupby(['username', 'dst', 'dst_port'])
feature_one = grouped_by_df.apply(lambda x: 'POST' in x.values).to_frame(name='feature_one')
feature_two = grouped_by_df.percent.min()
feature_two = feature_two.fillna(0)
feature_two = feature_two.to_frame(name='feature_two')
features_three = grouped_by_df.method.apply(lambda x: 'CONNECT' in x.values).to_frame(name='feature_three')
features = feature_one.merge(feature_two, left_index=True, right_index=True, how='left')
features.compute()
                       feature_one  feature_two
username dst     dst_port                          
a        1.1.1.1 80               True          0.3
b        1.1.1.1 80               True          0.3

features_full = features.merge(features_three, how='left', right_index=True, left_index=True)
features_full.compute()

where result is

Out[53]: 
Empty DataFrame
Columns: [feature_one, feature_two, feature_three]
Index: []

Computing features_three gives me the follwoing dataframe

feature_three.compute()
Out[28]: 
                           feature_three
username dst     dst_port               
a        1.1.1.1 80                False
b        1.1.1.1 80                False

Which have the same index as features dataframe above.

Using plain dataframes the result is correct that is feature_three column added to features:

features_df = features.compute()
features_three_df = features_three.compute()
features_all_df = features_df.merge(features_three_df, how='left', right_index=True, left_index=True)
features_all_df
Out[32]: 
                           feature_one  feature_two  feature_three
username dst     dst_port                                         
a        1.1.1.1 80               True          0.3          False
b        1.1.1.1 80               True          0.3          False

Shouldn't functionality of dask merge resemble the one on plain dataframes?

Activity

  1. charlesbluca commented on Jan 31, 2023

    @charlesbluca
    Member

    Sorry this didn't get much attention when initially opened 😕 running through your reproducer now with the latest Dask version as of now (2023.1.1), it looks like your reproducer now returns the expected result:

    In [4]: features_full = features.merge(features_three, how='left', right_index=True, left_index=True)
       ...: features_full.compute()
    Out[4]: 
                               feature_one  feature_two  feature_three
    username dst     dst_port                                         
    a        1.1.1.1 80               True          0.3          False
    b        1.1.1.1 80               True          0.3          False

    So I think we should be able to close this now, feel free to re-open if you run into a similar issue with a different reproducer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions