-2

I have a several question.

First, I want to datetime in pandas dataframe.

like this... 2018/03/06 00:01:27:744

How can I replace this datetime?

And Then.. Second..

        Time                  Sensor1         Sensor2    TimeCumsum
2018/03/06 00:01:27:744         0               1            
2018/03/06 00:01:27:759         0               1
2018/03/06 00:01:27:806         0               1            0.15
2018/03/06 00:01:27:838         1               1    
2018/03/06 00:01:28:009         1               1            0.2
2018/03/06 00:01:28:056         1               0            ...

I wanna Time Seconds cumsum when Sensor1 is 0 And Sensor2 is 1.

How can I do this?

Thanks.

GrayHash
  • 297
  • 2
  • 9
  • 1
    [The docs](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html) for time and [the docs](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html) for resample to help you get a cumsum. You'll want to do something like ```df = df.assign(seconds = df.dt.seconds)``` to get a column of seconds and then use ```.resample``` or ```.cumsum``` to get your answer. – lwileczek May 08 '18 at 12:21
  • Is that to_datetime use this?? milliseconds is that correct?? :( .. – GrayHash May 08 '18 at 12:27

1 Answers1

1

I believe need:

df['Time'] = pd.to_datetime(df['Time'], format='%Y/%m/%d %H:%M:%S:%f')

m = (df['Sensor1'].eq(0) & df['Sensor2'].eq(1))
df['col'] = df.loc[m, 'Time'].dt.microsecond.cumsum() // 10**3
print (df)
                     Time  Sensor1  Sensor2     col
0 2018-03-06 00:01:27.744        0        1   744.0
1 2018-03-06 00:01:27.759        0        1  1503.0
2 2018-03-06 00:01:27.806        0        1  2309.0
3 2018-03-06 00:01:27.838        1        1     NaN
4 2018-03-06 00:01:28.009        1        1     NaN
5 2018-03-06 00:01:28.056        1        0     NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252