0

I went into a problem when I round the timestamp to the nearest 15mins and append back to the dataframe, HMS wont show up, only showing the date. How do I fix it?

Orginal dataset:

        uid timestamp           loc app_id  traffic
2807379 941 2016-04-20 00:00:00 6049    2   0.001
2817702 942 2016-04-20 00:00:03 9803    350 0.001
2817701 942 2016-04-20 00:00:03 6406    15  0.000
424540  325 2016-04-20 00:00:03 2654    127 0.001

Round to the closest 15 min interval by:

round_15_min = usage["timestamp"].dt.round("15min")

print out round_15_min:

2807379   2016-04-20 00:00:00
2817702   2016-04-20 00:00:00
2817701   2016-04-20 00:00:00
424540    2016-04-20 00:00:00

However when append back to original dataset it became:

        uid timestamp           loc app_id  traffic rounded_timestamp
2807379 941 2016-04-20 00:00:00 6049    2   0.001   2016-04-20
2817702 942 2016-04-20 00:00:03 9803    350 0.001   2016-04-20
2817701 942 2016-04-20 00:00:03 6406    15  0.000   2016-04-20
424540  325 2016-04-20 00:00:03 2654    127 0.001   2016-04-20

dtype from datetime64[ns] became dtype('<m8[ns]') How do I fix this problem? To make it show HMS?

Nathan Chan
  • 147
  • 1
  • 13
  • Related: https://stackoverflow.com/q/29206612/10197418 - I think the h/m/s are just not *displayed*, which doesn't mean they're not there. – FObersteiner Mar 28 '21 at 09:51

1 Answers1

1

You can use the Datetime library :


import datetime
usage["timestamp"] = usage["timestamp"].apply(lambda dt: datetime.datetime(dt.year, dt.month, dt.day, dt.hour,15*(dt.minute // 15)))

For example,

        id                  dt
0  2807379 2016-04-20 00:00:00
1  2817702 2016-04-20 00:33:03
2  2817701 2016-04-20 00:09:03
3   424540 2016-04-20 00:50:03

will become :

        id                  dt
0  2807379 2016-04-20 00:00:00
1  2817702 2016-04-20 00:30:00
2  2817701 2016-04-20 00:00:00
3   424540 2016-04-20 00:45:00
Lue Mar
  • 442
  • 7
  • 10