0

I am reading a sheet from excel file that contains block of hours. Excel sheet

cls.model_slots_result = pandas.read_excel(model_static_results_xlsx, sheet_name='model_slots',
                                               engine='openpyxl')

When the time is 00:00:00, pandas load it as a Timestamp ('1899-12-30 00:00:00') i.e. Datetime in the dataframe even though other values (block of hours) of the same column are stored as datetime.time. Dataframe image

As of now I have created a function that checks if the type is datetime.time, if not it extracts time from datetime.

cls.model_slots_result['START_TIME'] = cls.model_slots_result['START_TIME'].apply(my_to_datetime)
cls.model_slots_result['END_TIME'] = cls.model_slots_result['END_TIME'].apply(my_to_datetime)

def my_to_datetime(datetime_obj):
   if not isinstance(datetime_obj, time):
      return datetime_obj.time()
   return datetime_obj

Is there any way to make pandas return datetime.time object for 00:00:00 instead of a datetime object or make pandas read the the column as datetime.time?

Thanks

Manan
  • 1
  • 2
  • Always provide a complete [mre] with code, **data, errors, current output, and expected output**, as **[formatted text](https://stackoverflow.com/help/formatting)**. If relevant, only plot images are okay. Please see [How to ask a good question](https://stackoverflow.com/help/how-to-ask) & you're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592) – Trenton McKinney Jan 27 '21 at 00:36
  • 1
    @TrentonMcKinney thank you for your feedback. It was my first question on this platform so had no clue. I have made the changes and hope it is much more clear. – Manan Jan 27 '21 at 01:18
  • Thank you for the updates to your question. Also don't use screen shots of data. Provide data with [How to provide a reproducible copy of your DataFrame using `df.head(15).to_clipboard(sep=',')`](https://stackoverflow.com/questions/52413246), then **[edit] your question**, and paste the clipboard into a code block. Because people will try to run your code and they don't want to retype your data. – Trenton McKinney Jan 27 '21 at 02:01

0 Answers0