0

I have a list of start and end times data of bike rides. Below are the first 5 entries of the list.

onebike_datetimes[0:4]

[{'start': datetime.datetime(2017, 10, 1, 15, 23, 25, tzinfo=tzfile('US/Eastern')),
  'end': datetime.datetime(2017, 10, 1, 15, 26, 26, tzinfo=tzfile('US/Eastern'))},
 {'start': datetime.datetime(2017, 10, 1, 15, 42, 57, tzinfo=tzfile('US/Eastern')),
  'end': datetime.datetime(2017, 10, 1, 17, 49, 59, tzinfo=tzfile('US/Eastern'))},
 {'start': datetime.datetime(2017, 10, 2, 6, 37, 10, tzinfo=tzfile('US/Eastern')),
  'end': datetime.datetime(2017, 10, 2, 6, 42, 53, tzinfo=tzfile('US/Eastern'))},
 {'start': datetime.datetime(2017, 10, 2, 8, 56, 45, tzinfo=tzfile('US/Eastern')),
  'end': datetime.datetime(2017, 10, 2, 9, 18, 3, tzinfo=tzfile('US/Eastern'))}]

I know for sure my list has some dates adjusted for day light savings. I am applying the following code to check if there are any dates that are ambiguous for day light savings start and end points. However, when did so, I am getting value error. Any suggestions, where could be the problem?

from dateutil import tz
for trip in onebike_datetimes:
  # Rides with ambiguous start
  if tz.datetime_ambiguous(trip['start']):
    print("Ambiguous start at " + str(trip['start']))
  # Rides with ambiguous end 
  if tz.datetime_ambiguous(trip['end']):
    print("Ambiguous end at " + str(trip['end']))

ValueError: Datetime is naive and no time zone provided.
Srinivas
  • 568
  • 1
  • 4
  • 21
  • Cannot reproduce the error if I run your code snipped. Had to replace `tzfile` with `dateutil.tz.gettz` to set the tzinfo of the datetime objects. Did you make sure that all the dt objects in onebike_datetimes are correctly localized? – FObersteiner May 18 '20 at 14:45
  • You are right. Upon more closer inspection, I have realised that some dates are not localised. Once corrected, it worked. Thank you. – Srinivas May 19 '20 at 07:48

0 Answers0