1

I am new in python. I have a column of datatime data. Now I want to create a new column with only time part of datetime in seconds.

my first row data is 2018-08-03 10:53:00 and I want to convert the 10:53:00 in seconds e.g 39180 want to store in new column.

My functionality is get the mean time from given datetime object. date can vary but I want mean time only.

James Z
  • 12,209
  • 10
  • 24
  • 44
Sandy
  • 985
  • 5
  • 13
  • post your sample data and expected output. – Sociopath Mar 09 '18 at 05:22
  • [This](https://stackoverflow.com/questions/48129251/pandas-dataframe-datetime-to-time-then-to-seconds) is the duplicate question with the same title and [this](https://stackoverflow.com/questions/19681703/average-time-for-datetime-list) is the one that answers your later question about finding the mean time of a datetime column. – Seth Rothschild Mar 09 '18 at 05:29

2 Answers2

1

You can use:

df = pd.DataFrame({
    'date': ['2018-08-03 10:53:00','2018-08-03 10:55:00'],
    'b': [10, 11]
})

#if necessary convert to datetime
df['date'] = pd.to_datetime(df['date'])
df['new'] = df['date'].dt.hour * 3600 + df['date'].dt.minute * 60 + df['date'].dt.second
print (df)
    b                date    new
0  10 2018-08-03 10:53:00  39180
1  11 2018-08-03 10:55:00  39300

mean = df['new'].mean()
print (mean)
39240.0

EDIT: If want mean ot time directly:

df['new'] = df['date'].dt.time
print (df)
    b                date       new
0  10 2018-08-03 10:53:00  10:53:00
1  11 2018-08-03 10:55:00  10:55:00

mean = df['new'].mean()
print (mean)

TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'

But mean of datetime is possible - convert to unix time in ns:

df['new'] = df['date'].values.astype(np.int64)
print (df)
    b                date                  new
0  10 2018-08-03 10:53:00  1533293580000000000
1  11 2018-08-03 10:55:00  1533293700000000000

mean = df['new'].mean()
print (mean)
1.53329364e+18
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • thanks man try to find but not getting but now understand and solved thanks man :) – Sandy Mar 09 '18 at 05:33
  • did in python directly have anything that will find the mean value of time component of date ? – Sandy Mar 09 '18 at 05:34
0

Here is an example of what you want to do

randdays = np.random.randint(1,100,10)
randhours = np.random.randint(1,100,10)
randdates = [datetime.now() + timedelta(days=int(i),hours=int(j)) for i,j in zip(randdays,randhours)]
sample_df = pd.DataFrame({'random_dates':randdates})
sample_df['seconds'] = sample_df.random_dates.apply(lambda x: (x-datetime(day=x.day,month=x.month,year=x.year)).seconds)
sample_df

output

    random_dates                seconds
0   2018-03-13 06:02:52.730957  21772
1   2018-06-17 10:02:52.730957  36172
2   2018-06-12 07:02:52.730957  25372
3   2018-05-09 05:02:52.730957  18172
4   2018-05-23 15:02:52.730957  54172
5   2018-03-29 21:02:52.730957  75772
6   2018-03-17 03:02:52.730957  10972
7   2018-05-16 07:02:52.730957  25372
8   2018-06-11 23:02:52.730957  82972
9   2018-03-10 14:02:52.730957  50572
vumaasha
  • 2,765
  • 4
  • 27
  • 41