1

After calling a file using pandas by this two lines:

import pandas as pd
import numpy as np
df = pd.read_csv('PN_lateral_n_eff.txt', header=None)
df.columns = ["effective_index"]

here is my output:

                         effective_index
0  2.568393573877396+1.139080496494329e-006i
1  2.568398351899841+1.129979376397734e-006i
2  2.568401556986464+1.123872317134941e-006i

after that, i can not use the numpy to convert it into a real number. Because, panda dtype was object. I tried this:

np.real(df, dtype = float)

TypeError: real() got an unexpected keyword argument 'dtype'

Any way to do that?

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Khalilrazu
  • 31
  • 2

1 Answers1

1

Looks like astype(complex) works with Numpy arrays of strings, but not with Pandas Series of objects:

cmplx = df['effective_index'].str.replace('i','j')\ # Go engineering
                             .values\               # Go NumPy
                             .astype('str')\        # Go string
                             .astype(np.complex)    # Go complex
#array([ 2.56839357 +1.13908050e-06j,  2.56839835 +1.12997938e-06j,
#        2.56840156 +1.12387232e-06j])
df['effective_index'] = cmplx # Go Pandas again
DYZ
  • 55,249
  • 10
  • 64
  • 93