yongsheng
yongsheng

Reputation: 396

Use np.nan without importing numpy

I am used to replacing empty string with NaN and dropping to remove empty data.

import pandas as pd
import numpy as np

df.replace('', np.nan).dropna()

However, I want my function to run using serverless framework. I need to import numpy just to use np.nan, which eats up my precious 250MB limit for package size.

Importing pd.np.nan works, but there is warning that pandas.np module is deprecated and will be removed from a future version of pandas.

Is there any solution to use np.nan without importing numpy?

Upvotes: 3

Views: 2241

Answers (4)

Cam
Cam

Reputation: 1763

Why not just import nan?

from numpy import nan

df.replace('', nan).dropna()

Upvotes: 0

Federico
Federico

Reputation: 1

Maybe I'm missing something obvious, but df.replace('', None).dropna() should work, right?

Upvotes: 0

Isak
Isak

Reputation: 56

Use pd.NA instead.

From the Docs:

Starting from pandas 1.0, an experimental pd.NA value (singleton) is available to represent scalar missing values. At this moment, it is used in the nullable integer, boolean and dedicated string data types as the missing value indicator. The goal of pd.NA is provide a “missing” indicator that can be used consistently across data types (instead of np.nan, None or pd.NaT depending on the data type).

Upvotes: 4

norok2
norok2

Reputation: 26906

Perhaps you can get away with math:

import math


math.nan
# nan

or even without importing anything:

float('nan')
# nan

These are compatible with NumPy's nan:

import numpy as np


np.isnan(math.nan)
# True

np.isnan(float('nan'))
# True

Upvotes: 7

Related Questions