Pandas reindex Dates To Subset of Dates from List

94 Views Asked by At

I am sorry, but there is online documentation and examples and I'm still not understanding. I have a pandas df with an index of dates in datetime format (yyyy-mm-dd) and I'm trying to resample or reindex this dataframe based on a subset of dates in the same format (yyyy-mm-dd) that are in a list. I have converted the df.index values to datetime using:

dfmla.index = pd.to_datetime(dfmla.index)

I've tried various things and I keep getting NaN's after applying the reindex. I know this must be a datatypes problem and my df is in the form of:

df.dtypes
Out[30]: 
month                int64
mean_mon_flow      float64
std_mon_flow       float64
monthly_flow_ln    float64
std_anomaly        float64
dtype: object

My data looks like this:

df.head(5)
Out[31]: 
            month  mean_mon_flow  std_mon_flow  monthly_flow_ln  std_anomaly
date                                                                        
1949-10-01     10       8.565828      0.216126         8.848631     1.308506
1949-11-01     11       8.598055      0.260254         8.368006    -0.883938
1949-12-01     12       8.612080      0.301156         8.384662    -0.755149
1950-08-01      8       8.614236      0.310865         8.173776    -1.416887
1950-09-01      9       8.663943      0.351730         8.437089    -0.644967

My month_list (list datatype) looks like this:

month_list[0:2]
Out[37]: ['1950-08-01', '1950-09-01']

I need my condensed, new reindexed df to look like this:

            month  mean_mon_flow  std_mon_flow  monthly_flow_ln  std_anomaly
date                                                                        
1950-08-01      8       8.614236      0.310865         8.173776    -1.416887
1950-09-01      9       8.663943      0.351730         8.437089    -0.644967

thank you for your suggestions,

1

There are 1 best solutions below

1
Quang Hoang On

If you're certain that all month_list are in the index, you can do df.loc[month_list], else you can use reindex:

df.reindex(pd.to_datetime(month_list))

Output:

            month  mean_mon_flow  std_mon_flow  monthly_flow_ln  std_anomaly
date                                                                        
1950-08-01      8       8.614236      0.310865         8.173776    -1.416887
1950-09-01      9       8.663943      0.351730         8.437089    -0.644967