pythonpandasindexingappendseries

How do I replace set_value with at[] in a pandas Series


I'm trying to construct a pandas Series to concatenate onto a dataframe.

import numpy as np
import pandas as pd

rawData = pd.read_csv(input, header=1) # the DataFrame

strikes = pd.Series()     # the empty Series
for i, row in rawData.iterrows():
    sym = rawData.loc[i,'Symbol']
    strike = float(sym[-6:])/1000
    strikes = strikes.set_value(i, strike)
print("at26: ",strikes.values)

This program works, but I get the error message:

line 25: FutureWarning: set_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead.

Every way I have tried to substitute .at, I get a syntax error. Many of the suggestions posted relate to DataFrames, not Series. Append requires another series, and complains when I give it a scalar.

What is the proper way to do it?


Solution

  • Replace strikes.set_value(i, strike) with strikes.at[i] = strike.

    Note that assignment back to a series is not necessary with set_value:

    s = pd.Series()
    
    s.set_value(0, 10)
    s.at[1] = 20
    
    print(s)
    
    0    10
    1    20
    dtype: int64
    

    For the algorithm you are looking to run, you can simply use assignment:

    strikes = rawData['Symbol'].str[-6:].astype(float) / 1000