Skip to content

pandas.core.indexes.period.PeriodIndex error when returning pandas df to excel using Run_Python #1084

@deltahedgetech

Description

@deltahedgetech

OS (e.g. Windows 10 or macOS Sierra)

mac

Versions of xlwings, Excel and Python (e.g. 0.11.8, Office 365, Python 3.7)

0.15.5

Describe your issue (incl. Traceback!)

Trying to return two data frames to excel
Looking at the code below, summ_df to B2 works and displays in excel.

It is the final_df in B12 that causes xlwings to error.
This code works when running in standalone mode i.e. outside of xlwings and returns both dataframes

# Your traceback here
Traceback (most recent call last):

  File "/Users/stuartclark/anaconda3/lib/python3.6/site-packages/aeosa/aem/aemcodecs.py", line 322, in pack

    return self.encoders[data.__class__](data) # quick lookup by type/class

KeyError: <class 'pandas.core.indexes.period.PeriodIndex'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "/Users/stuartclark/Documents/PortfolioManagement/Libraries/manu_pmi.py", line 99, in main

    sht.range('B12').value = final_df.index

  File "/Users/stuartclark/anaconda3/lib/python3.6/site-packages/xlwings/main.py", line 1525, in value

    conversion.write(data, self, self._options)

  File "/Users/stuartclark/anaconda3/lib/python3.6/site-packages/xlwings/conversion/__init__.py", line 43, in write

    pipeline(ctx)

  File "/Users/stuartclark/anaconda3/lib/python3.6/site-packages/xlwings/conversion/framework.py", line 66, in __call__

    stage(*args, **kwargs)

  File "/Us��Êèˇ

Include a minimal code sample to reproduce the issue (and attach a sample workbook if required!)

# Your code here

    df = df1.fillna(method='ffill')    
    AVG_12M = pd.DataFrame(df.loc[M12:today].mean(),columns=['12M Avg']).round(2)
    AVG_3M=pd.DataFrame(df.loc[M3:today].mean(),columns=['3M Avg']).round(2)
    MIN_12M=pd.DataFrame(df.loc[M12:today].min(),columns=['Min 12M']).round(2)
    MAX_12M = pd.DataFrame(df.loc[M12:today].max(),columns=['Min 12M']).round(2)
    MM=np.subtract(df[-1:],df[-2:-1]).T.round(2)
    MM.columns = ['M/M']
    LAST_AVG12M = np.subtract(df[-1:].T, AVG_12M).round(2)
    LAST_AVG12M.columns = ['Latest-12M Avg']
    
    summ_df=pd.concat([AVG_12M,AVG_3M,MIN_12M,MAX_12M,LAST_AVG12M,MM],axis=1).T
    #display(summ_df)
    #display(df1.iloc[::-1].head(15))
    
    sht.range('B2').value = summ_df
    
    final_df = df1.iloc[::-1].head(15).dropna()
    
    sht.range('B12').value = final_df

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions