1

I have a date in a list:

[datetime.date(2017, 8, 9)]

I want replace the value of a dataframe matching that date with zero.

Dataframe:

          Date  Amplitude    Magnitude  Peaks  Crests
0   2017-06-21   6.953356  1046.656154      4       3
1   2017-06-27   7.015520  1185.221306      5       4
2   2017-06-28   6.947471   908.115055      2       2
3   2017-06-29   6.921587   938.175153      3       3
4   2017-07-02   6.906078   938.273547      3       2
5   2017-07-03   6.898809   955.718452      6       5
6   2017-07-04   6.876283   846.514852      5       5
7   2017-07-26   6.862897   870.610086      6       5
8   2017-07-27   6.846426   824.403786      7       7
9   2017-07-28   6.831949   813.753420      7       7
10  2017-07-29   6.823125   841.245427      4       3
11  2017-07-30   6.816301   846.603427      5       4
12  2017-07-31   6.810133   842.287006      5       4
13  2017-08-01   6.800645   794.167590      3       3
14  2017-08-02   6.793034   801.505774      4       3
15  2017-08-03   6.790814   860.497395      7       6
16  2017-08-04   6.785664   815.055002      4       4
17  2017-08-05   6.782069   829.607640      5       4
18  2017-08-06   6.778176   819.014799      4       3
19  2017-08-07   6.774587   817.624203      5       5
20  2017-08-08   6.771193   815.101641      4       3
21  2017-08-09   6.765695   772.970000      1       1
22  2017-08-10   6.769422   945.207554      1       1
23  2017-08-11   6.773154   952.422598      4       3
24  2017-08-12   6.770926   826.700122      4       4
25  2017-08-13   6.772816   916.046905      5       5
26  2017-08-14   6.771130   834.881662      5       5
27  2017-08-15   6.769183   826.009391      5       5
28  2017-08-16   6.767313   824.650882      5       4
29  2017-08-17   6.765894   832.752100      5       5
30  2017-08-18   6.766861   894.165751      5       5
31  2017-08-19   6.768392   912.200274      4       3

i have tried this:

for x in range(len(all_details)):
    for y in selected_day:
        m = all_details['Date'] > y
        all_details.loc[m, 'Peaks'] = 0

But getting an error:

ValueError: Arrays were different lengths: 32 vs 1

Can anybody suggest me the correct way to do it> Any help would be appreciated.

Rakesh
  • 81,458
  • 17
  • 76
  • 113
Dheeraj
  • 1,102
  • 3
  • 14
  • 29

1 Answers1

1

First your solution working nice with your sample data.

Another faster solution is creating each mask in loop and then reduce by logical or, and - what need. Better it is explained here.

L = [datetime.date(2017, 8, 9)]
m = np.logical_or.reduce([all_details['Date'] > x for x in L])
all_details.loc[m, 'Peaks'] = 0

In your solution is better compare only by minimal date from list:

all_details.loc[all_details['Date'] > min(L), 'Peaks'] = 0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252