0

I'm finding that when adding a series, based on the same time-period, to an existing dataframe it gets imported as NaNs. The dataframe has a field column, but I don't understand why that should change anything. To see the steps of my code, you can review the attached image. Hope that someone can help!

Illustration showing how the dataframe that the series is inserted into and how it gets read as NaN

johan bender
  • 53
  • 1
  • 1
  • 5
  • 2
    Please provide a [mcve]. See also [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). This means no links or images, just text. – jpp Jun 14 '18 at 07:58
  • the indexes are not the same. What about actual? please provide information as code not as images – Quickbeam2k1 Jun 14 '18 at 08:01
  • Thank you for the link jpp! It's difficult for me to reproduce as the data is quite complex. @Quickbeam2k1: You're right that one data frame is a multi-index and the other just a series. However, the field 'Actual' is not relevant at all. Any way to overwrite it so that I can add the series – johan bender Jun 14 '18 at 08:09
  • @johan, we all have the problem, just try to create some dummy data – Quickbeam2k1 Jun 14 '18 at 08:23

1 Answers1

0

Assuming that the value in the Field Index column is "actual" for every row, a solution could be the following:

test.reset_index().set_index('Date').assign(m1=m1)

That solution works but it can be done shorter:

days = pd.to_datetime(['2018-01-31', '2018-02-28', '2018-03-31'])
df = pd.DataFrame({'Field': ['Actual']*3, 'Date': days, 'Val':[1, 2, 3]}).set_index(['Field', 'Date'])
m1 = pd.Series([0, 2, 4], index=days)

df.reset_index(level='Field').assign(m1=m1)
    Field   Val m1
Date            
2018-01-31  Actual  1   0
2018-02-28  Actual  2   2
2018-03-31  Actual  3   4

btw, that would be a nice mcve

Quickbeam2k1
  • 5,287
  • 2
  • 26
  • 42