Skip to content Skip to sidebar Skip to footer

Pandas Shift Converts My Column From Integer To Float.

shift converts my column from integer to float. It turns out that np.nan is float only. Is there any ways to keep shifted column as integer? df = pd.DataFrame({'a':range(5)}) df['b

Solution 1:

Solution for pandas under 0.24:

Problem is you get NaN value what is float, so int is converted to float - see na type promotions.

One possible solution is convert NaN values to some value like 0 and then is possible convert to int:

df = pd.DataFrame({"a":range(5)})
df['b'] = df['a'].shift(1).fillna(0).astype(int)
print (df)
   a  b
0  0  0
1  1  0
2  2  1
3  3  2
4  4  3

Solution for pandas 0.24+ - check Series.shift:

fill_value object, optional The scalar value to use for newly introduced missing values. the default depends on the dtype of self. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used.

Changed in version 0.24.0.

df['b'] = df['a'].shift(fill_value=0)

Solution 2:

another solution starting from pandas version 0.24.0: simply provide a value for the parameter fill_value:

df['b'] = df['a'].shift(1, fill_value=0)

Solution 3:

You can construct a numpy array by prepending a 0 to all but the last element of column a

df.assign(b=np.append(0, df.a.values[:-1]))

   ab000110221332443

Solution 4:

As of pandas 1.0.0 I believe you have another option, which is to first use convert_dtypes. This converts the dataframe columns to dtypes that support pd.NA, avoiding the issues with NaN.

df = pd.DataFrame({"a":range(5)})
df = df.convert_dtypes()
df['b'] = df['a'].shift(1)

print(df['a'])
# 0    0# 1    1# 2    2# 3    3# 4    4# Name: a, dtype: Int64print(df['b'])
# 0    <NA># 1       0# 2       1# 3       2# 4       3# Name: b, dtype: Int64

Solution 5:

another solution is to use replace() function and type cast

df['b'] = df['a'].shift(1).replace(np.NaN,0).astype(int)

Post a Comment for "Pandas Shift Converts My Column From Integer To Float."