wuwucat
wuwucat

Reputation: 2621

day is out of range for month python

'reobsmaf' is a DataFrame as below:

In [152]: reobsmaf[:5]
Out[152]: 
          yy  mm  dd  count      mean    median       min       max       std
2001-01  2001   1  15     76       NaN       NaN       NaN       NaN       NaN
2001-02  2001   2  15    672  5.410384  5.388889  4.111111  6.611111  0.484927
2001-03  2001   3  15    744  3.956317  3.666667  2.888889  5.555556  0.612632
2001-04  2001   4  15    720  3.685185  3.666667  3.333333  3.833333  0.118767
2001-05  2001   5  15    744  3.846550  3.833333  3.666667  4.444444  0.175195

'remodmaf' is another DataFrame as below:

In [153]: remodmaf[:5]
Out[153]: 
          yy  mm  dd  count      mean    median       min       max       std
2001-01  2001   1  15     75       NaN       NaN       NaN       NaN       NaN
2001-02  2001   2  15    673       NaN       NaN       NaN       NaN       NaN
2001-03  2001   3  15    745  4.508178  4.601572  3.117909  6.313059  0.925472
2001-04  2001   4  15    721  5.402154  5.510775  4.015624  5.923597  0.469602
2001-05  2001   5  15    745  5.352246  5.311082  5.104457  5.892380  0.186441

now I want to compare these two DataFrames, if which record's 'mean' value in reobsmaf equals 'nan',the record's 'mean' value in remodmaf equals 'nan' either.as below:

        for i in range(len(reobsmaf)):
          if str(reobsmaf['mean'][i])=='nan'
             remodmaf['mean'][i]= 'nan'

it raise an exception:

DateParseError: day is out of range for month

I know it's an specific problem but I really don't know what's the problem is.

Upvotes: 1

Views: 1254

Answers (1)

Jeff
Jeff

Reputation: 128948

If am reading your question correctly, you want to assign NaN in one frame where the other is Nan?

In [4]: df = pd.DataFrame(np.random.randn(8,3),columns=['A','B','C'])
In [9]: df2 = df.copy()

In [10]: df2.ix[0:3,['B','C']] = np.nan

In [11]: df2
Out[11]: 
          A         B         C
0  0.404500       NaN       NaN
1  1.391802       NaN       NaN
2 -0.365778       NaN       NaN
3  0.693149       NaN       NaN
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364

In [12]: df
Out[12]: 
          A         B         C
0  0.404500 -0.222471  0.864739
1  1.391802 -0.503080  0.307754
2 -0.365778  0.530288 -0.743119
3  0.693149 -0.749732 -1.135363
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364

This is a where operation. where df2 is nan, set df to nan

In [14]: df[pd.isnull(df2)] = np.nan

In [15]: df
Out[15]: 
          A         B         C
0  0.404500       NaN       NaN
1  1.391802       NaN       NaN
2 -0.365778       NaN       NaN
3  0.693149       NaN       NaN
4  0.233268  0.332789 -0.130531
5 -0.531822  1.161906  0.210007
6 -0.507082 -2.050773 -1.258930
7  0.521823 -0.331544 -0.926364

Upvotes: 1

Related Questions