1

I have a dataset with time. Here My first time is 6:00:00 a.m. Here I want to replace every start time into 0 minutes. I checked the answers of "Convert timedelta to total seconds" and the answers are not related to my question. I tried so many replace code. But it didn't work. Can anyone help me to solve this problem? Here I upload the code what I tried?

dataset = pd.read_csv('l.csv')
data= pd.DataFrame(dataset,columns=['date','time'])
dtwithoutseconds = dt.replace(second=0, microsecond=0)

my dataset:

My csv file.

enter image description here

As you can see in the image , in every start time in every day should be the 0 minutes. After replacing the start time into 00:00:00 then add 60 minutes 60 minutes till to next day start time.

start_time = 0 minutes

second_time = 60 minutes   

third_time =  120 minutes

till to 

next_day start time again = 0 minutes

Error is coming while typing CSMaverick code. Here I upload the image of the error:

enter image description here

According to the cs95 last code, it gave me a big error

dataset = pd.read_csv('l.csv')
data= pd.DataFrame(dataset,columns=['date','time'])
data['time'] -= data.at[0, 'time']; data['time_in_sec'] =    data['time'].dt.total_seconds()
 print(data['time'])

Error:

enter image description here

enter image description here

  • 2
    df.assign(Date=df.Date.dt.round('H')) – Chandu Jun 24 '19 at 16:30
  • http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.round.html – Chandu Jun 24 '19 at 16:30
  • Please indicate your expected output. Should it be [0:00:00, 1:00:00, ...]? – cs95 Jun 24 '19 at 16:33
  • @cs95 Yes, after replacing start time into 0 seconds. Then add 60 minutes into it (one hour) for next time. [00:00:00 , 01:00:00,02:00:00............... next day (start time) = 00:00:00 ] –  Jun 24 '19 at 16:42
  • 2
    Does `df['time'] = pd.to_timedelta(df['time']); df['time'] -= df.at[0, 'time']`? – cs95 Jun 24 '19 at 16:43
  • @cs95 Yes It's working. Thank you for helping me. Can I ask one question from you? If I want to display it in minutes, then what I have to change in you code? –  Jun 24 '19 at 16:57
  • Can you please edit your question to contain text instead of an image, and indicate your expected output? I will write an answer with the info you're asking for. – cs95 Jun 24 '19 at 16:59
  • @CSMaverick When I tried your code It gave me an error " Can only use .dt accessor with datetimelike values" . Here I paste the image of error. –  Jun 24 '19 at 17:00
  • @cs95 Yes I edited my question. Not in a minutes, Sorry I just want to show my time in seconds. I hope you can understand it and you will help me to solve this with new code. –  Jun 24 '19 at 17:11
  • Is `df['time'].dt.total_seconds()` what you need? – cs95 Jun 24 '19 at 17:19
  • @cs95 So I have to write this after your earlier code isn't it? But If I wrote this nothing changed It is displaying same as one hour. [00:00:00 , 01:00:00, 02:00:00, 03:00:00, 04:00:00, 05:00:00 , 06:00:00, 07:45:00, 09:00:00, 10:00:00,11:00:00, 12:00:00] –  Jun 24 '19 at 17:24
  • 1
    Full code: `df['time'] -= df.at[0, 'time']; df['time_in_sec'] = df['time'].dt.total_seconds()` – cs95 Jun 24 '19 at 17:26
  • @cs95 Sorry when I typed your code it gave me a big error. Here I upload it –  Jun 24 '19 at 17:32
  • 1
    You should have also run `df['time'] = pd.to_timedelta(df['time'])` before running the code above. – cs95 Jun 24 '19 at 17:33
  • @cs95 I tried it, error gone, but results are same. Nothing chsnged. It didn't convert into seconds. It is displaying one hour one hour. –  Jun 24 '19 at 17:39
  • Yeah, sorry, it worked for me. You are checking the wrong column or whatever. – cs95 Jun 24 '19 at 17:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195476/discussion-between-awa-and-cs95). –  Jun 24 '19 at 17:42

0 Answers0