From: Benjamin R. <ben...@ou...> - 2011-09-29 15:38:49
|
On Thu, Sep 29, 2011 at 9:33 AM, Chao YUE <cha...@gm...> wrote: > Dear all, I have a variable d which has several years plus 11 month data > (len(d)%12=11). so I want to append a NaN to the data so that it constitutes > complete > several years of data. > > but I cannot use "d=np.concatenate(d,np.array(np.nan))" to finish this job. > > > another question, is there a simple function like is.Nan(ndarray) in numpy > to check the missing value? and how can I get the index of the missing > value? > > Thanks a lot, > > Chao > > In [246]: d > Out[246]: > array([-24. , -12.9, -14. , 4.2, 7.3, 12.9, 18.5, 16.9, 10.7, > 7.6, -1.9, -9.8, -12. , -16.6, -13.3, 5. , 12.2, 14. , > 16.5, 15.6, 11.8, 6.4, -6.6, -14.8, -17.6, -15.4, -3.7, > -2.1, 6.9, 12.8, 17. , 16.7, 9. , 4.5, -11.9, -18.1, > -18.4, -19.5, -3.4, 6.4, 8.5, 13.5, 18.3, 16.5, 10.4, > 1.7, -9.6, -19.5, -17.8, -20.9, -10.9, 0.8, 7.7, 14.7, > 20.4, 16.2, 9.1, 6.9, -8.6, -17.1, -16.6, -20.8, -14.1, > -4.7, 10.7, 15.8, 18.5, 17.1, 12.9, 5.2, -6. , -18.4, > -20.5, -22.6, -8.3, 7. , 10.9, 14. , 17.1, 18.6, 10.1, > 2.1, -1.1, -20.8, -32.9, -17.7, -12.2, -0.4, 9.5, 14.6, > 17.3, 15.1, 11.1, 2.6, -9.9, -16.9, -22.9, -18.1, -15.4, > 0.5, 11. , 12.4, 16.9, 14.7, 9.7, -0.3, -10.7, -20.7, > -23.1, -13.5, -10.7, 7.6, 11. , 14.4, 16.8, 15.6, 11.2, > 4.2, -3.5, -12.2, -19.9, -13.2, -10.1, -0.1, 8.5, 13.6, > 17.2, 17.6, 10.1, 6.3, -2.3, -12.4, -27.2, -9.1, -13. , > -6.2, 7.4, 12.9, 17.3, 14.8, 9.5, 4.3, -1.1, -8.6, > -17.5, -18.5, -15.9, 3.5, 9.6, 15.7, 18.4, 17.1, 9.4, > 4.5, -15.1, -21.9, -21.2, -18.5, -12.2, -2.1, 10.4, 16.7, > 17. , 16.1, 8.6, 3.9, -4.8, -16. , -20.9, -16.8, -9.3, > 0.9, 10.3, 13.8, 19. , 15.3, 11.1, 3.1, -5.5, -14.9, > -12.8, -17.5, -6.9, 3.7, 11.1, 12.5, 16.5, 16.5, 10.2, > 4.2, -8.8, -16.3, -24.4, -17.2, -4.7, 3.5, 8. , 14. , > 18.3, 14.6, 8.9, -0.4, -9.1, -8.4, -20.8, -15.5, -12. , > 2.3, 9.8, 13.6, 18.4, 16.8, 11.9, 3.9, -8. , -13.6, > -17.4, -13.1, -6.9, 0.2, 10.6, 17.6, 17.2, 18.8, 6.3, > 3.4, -7.6, -22.7, -19.5, -20.9, -11.7, 1.7, 9.8, 16.6, > 16.4, 16.2, 10.6, 6.1, -3.6, -14.4, -22.2, -17.8, -7.5, > 3.2, 8.9, 14.8, 18.6, 17.7, 12.9, 8.4, -6.9, -16.8, > -16.4, -10.1, -15.6, 3.5, 11.2, 14.7, 19.5, 14.6, 8.3, > 4.4, -8.2, -22.3, -23. , -21.8, -14.1, 2.5, 9.1, 15. , > 17.9, 16.4, 5.3, 5.2, -11.6, -14.1, -29. , -20.5, -8.1, > -0.7, 10.1, 13.9, 17.3, 15.5, 12.4, 3.4, -13.3, -15.1, > -21. , -19.5, -12.5, -2.4, 8.9, 13.5, 17.6, 17.3, 14.4, > 2.7, -5.1, -15.5, -20.7, -14.5, -3.2, 2.7, 9.6, 14.3, > 16. , 13.7, 10.9, 3.2, -4.1, -18.6, -29.2, -16.7, -11. , > 5.1, 9.2, 12.5, 16.2, 17.5, 10.2, -0.8, -5.3, -11.4, > -20.9, -15. , -12.8, 2.1, 8.8, 16.9, 18.2, 16.1, 9.9, > 3.4, -9. , -23.5, -25.2, -15.4, -10.3, 2.5, 11.7, 14.5, > 15.4, 17.8, 9.9, 3.5, -7.4, -20.7, -25.1, -22.6, -9.7, > 1.2, 11.6, 16.3, 14.8, 17.4, 5.9, 0.9, -6.8, -21.4, > -16.6, -15.8, -4.1, 1.7, 10.9, 14.5, 16.8, 16.8, 9.8, > 4.2, -12.8, -18.5, -25.2, -16.8, -14.6, 2.4, 6.2, 14.9, > 17.9, 13.6, 7.6, 5.3, -4.2, -8.5, -17.5, -17.7, -13.4, > 0.1, 8.8, 14.3, 19.3, 14. , 10. , 3.5, -6.1, -16.8, > -16.5, -12.5, -10. , 5.8, 11.6, 14.4, 17.7, 16.7, 11.6, > 1.4, -7.3, -16. , -22.2, -7.8, -5.1, 6. , 13.6, 15.5, > 16.3, 12.3, 9.8, 5.2, -7.9, -21.1, -24.2, -17.4, -7.6, > 2.5, 11.7, 15.4, 16.7, 14.4, 10.6, 5.9, -10.4, -18. , > -21.5, -25.6, -10.2, -2.9, 7.8, 15. , 19.2, 15.3, 11.3, > 4.4, -6. , -14.4, -21. , -15.4, -11.5, 7.4, 13.3, 15.2, > 17.6, 15.1, 9.3, 5. , -4.4, -19.4, -12.6, -11.4, -2.6, > 3.2, 10.9, 14.4, 17.9, 19.1, 11.8, 1.9, -0.9, -14.2, > -29.7, -17.6, -9.6, 0.5, 9.2, 14.3, 17.7, 14.6, 10.1, > 4.2, -10.7, -14.4, -14.5, -13.1, -8.5, 0.5, 8. , 15.1, > 18.8, 19.4, 9.3, 4.4, -3.2, -23.6, -14.2, -6.7, -8. , > 6.8, 9. , 15.8, 18.6, 18.8, 7.6, 2.2, -11.3, -21.2, > -17.2, -19.5, -4.9, 4.3, 11.8, 12.3, 17.1, 14.3, 7.7, > 3. , -16.7, -14.2, -11.7, -16.4, -3.8, 2.1, 12.1, 15.3, > 16.9, 16.2, 8.5, 4.7, -10.7, -10.7, -11.6, -10.2, -6.2, > 6.4, 12.7, 17.9, 17.3, 13.7, 12.6, 3.6, -2.7, -10.8, > -20.3, -15.3, -6.8, 4. , 13.9, 19.3, 18.5, 15.8, 10.9, > 3. , -5.3, -12.3, -15.7, -16.7, -12.3, 3.5, 10.7, 16. , > 19.8, 17.1, 10.8, 4.1, -9.1, -18.6, -15.9, -17.7, -4.2, > 2.6, 10.3, 16.4, 17.3, 16.9, 12.2, 1.7, -9.7, -21.7, > -19.3, -10.7, -9.5, 4.7, 11.3, 16.5, 18.5, 19.8, 10.6, > -0.3, -10.2, -14.2, -13.2, -11.1, -1.8, 3.4, 9.3, 14. , > 16.1, 14.2, 8.4, 3.2, -3.1, -20.8, -18.3, -14.1, -1.5, > 3.9, 11. , 12.8, 15.6, 15.8, 8.7, 2.9, -7.4, -11.3, > -23.2, -21.6, -2. , 3.8, 10.3, 15. , 17.1, 15.6, 12.9, > 4.6, -8. , -15.6, -16.1, -14.1, -8.4, -1.1, 9.3, 16.9, > 16.9, 15.7, 11.4, 4.3, -11.3, -17.1, -25. , -14. , -11.6, > 1.4, 7.9, 16.2, 17.6, 17.9, 10. , 2.6, -12.5, -20.1, > -22.3, -11.9, -10.2, 0.4, 9.2, 16.6, 18.4, 17.8, 12.6, > 3. , -4.7, -6. , -19.3, -6.1, -7.8, 6.6, 12.3, 14.4, > 18.5, 18.9, 12.1, 4.1, -5.6, -14.7, -18.2, -11.7, -4.8, > 5.6, 9.9, 13.8, 16.5, 17. , 10.1, 3.2, -3.3, -9. , > -19.7, -11.9, -3.4, 3.1, 8.9, 13.4, 17.6, 15.9, 10.8, > 4.4, -6.9, -21. , -9.8, -18. , -4.1, 3.6, 11.6, 14.8, > 18.7, 18.7, 12.8, 1.7, -2.2, -13.9, -16.8, -9.9, -14.6, > -1.7, 7.6, 17.4, 20.3, 16.2, 10.2, -1.9, -6.3, -10.5, > -18.5, -18.6, -10.1, 3.8, 11.7, 15.7, 18.5, 19.6, 10.1, > 5.6, -9.5, -11.4, -21.5, -11.2, NaN, 3.5, NaN, 13. , > 16.8, 13.9, 10.1, 2.7, -2.9, -15.8, -18.8, -12.6, -6.3, > 5.7, 9.5, 14.2, 17.5, 14.9, 10.8, 4. , -3.4, -9.3, > -8.7, -13. , -8.1, 6.8, 11. , 16.6, 19.5, 17.3, 11.5, > 1.5, -10.4, -11.9, -14.2, -18.3, -6.1, 2.9, 10.6, 14.3, > 20.6, 14.9, 10. , 4.8, -7.5]) > > Here is a quick-and-dirty way to do what you want: d_full = np.empty(len(d) + 1) d_full[:len(d)] = d d_full[-1] = np.nan As for testing for NaNs, use "np.isnan()". That returns a boolean array the same shape as the input array (which can be used for boolean indexing). However, if you really need the indices, then you can use "np.nonzero(np.isnan(d_full))[0]". Note the use of "[0]" because np.nonzero returns a tuple of information, but you usually only need the first one. I hope that helps! Ben Root |