简体   繁体   中英

Is there a setting or way to have pandas warn or alert when doing operations with dataframes that are not the same size?

The following snippet is an example of an operation I would like an alert or warning for. Not sure if there is some methodology or setting in pandas to avoid situations like this.

import pandas as pd
import numpy as np
df = pd.DataFrame([1,2,3,4,5,-6,-8,-9],columns=['nums'])
np.sign(df) * df[df['nums'] > 0]

   nums
0   1.0
1   2.0
2   3.0
3   4.0
4   5.0
5   NaN
6   NaN
7   NaN

np.sign(df) will be longer than df[df['nums'] > 0] resulting in NaN s appended and a mismatched multiplication

I think I misunderstood how pandas handles situations like this. np.sign(df) has the full index RangeIndex(start=0, stop=8, step=1) , but df[df['nums'] > 0] has a smaller index Int64Index([0, 1, 2, 3, 4], dtype='int64') . The indices are used to decide what to multiple what to what, so this issue isn't as bad as I originally thought. I guess a isna() check afterwards would suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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