Just learning Python and this is puzzling me

Is there no constants in Python? Otherwise why can you do this??

import numpy as np
np.nan = 0
print(np.nan == np.nan, np.isnan(np.nan))  # True False

The operator can return different type depending on input and you have to check the return type. And I guess someone thought it is a great idea designing it?

df = pd.DataFrame([{'Name': 'Alice'},
                   {'Name': 'Jack'},
                   {'Name': 'Helen'}],
                   index=['group1','group1','group2'])
print(type(df.loc['group1'])) # pandas.core.frame.DataFrame
print(type(df.loc['group2'])) # pandas.core.series.Series

print(type(df.loc[df.index == 'group2'])) # workaround to always get DataFrame

Another way to treat missing values (the most illogical as to me)

print(np.nan == np.nan, np.nan != np.nan) # False True

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s