1/28/2024 0 Comments Datetime.now minus minutes pythonSo like mentioned by many above to get the actual value of the difference in minute you have to do: df=df/60īut if just want the difference between the minute parts of the two timestamps then do the following #convert the timedelta to datetime and then extract minuteĭf=(pd.to_datetime(df-df)).dt.minuteĪnd see section 8.1.2 you'll see the read only attributes are only seconds,days and milliseconds. ![]() Whereas the column generated from subtracting two datetimes has formatĪttributeError: 'TimedeltaProperties' object has no attribute 'm8' Now minute is tricky as dt.minute works only on datetime64 dtype. If you need it in days or seconds then # For difference in days:ĭf=(df - df).dt.daysĭf=(df - df).dt.seconds If you are trying to find the difference between timestamps that are in pandas columns, the the answer is fairly simple. If you can't install 3rd party modules then time.mktime() could be used from Cochrane's answer that can find the correct utc offset on some platforms for some dates in some timezones - if you don't need a consistent (but perhaps wrong) result then it is much better than doing dt2 - dt1 with naive datetime objects that always fails if the corresponding utc offsets are different. You could set is_dst=None to raise an exception for ambiguous or non-existent local times instead. If either dt1 or dt2 correspond to an ambiguous time then the default is_dst=False is used to disambiguate. Td = aware_dt2 - aware_dt1 # elapsed time Local_tz = tzlocal.get_localzone() # get pytz timezoneĪware_dt1, aware_dt2 = map(local_tz.localize, ) The portable way to make an aware local datetime objects is to use pytz timezones: #!/usr/bin/env python If the input time is in the local timezone that might have different utc offset at different times e.g., it has daylight saving time then you should make dt1, dt2 into aware datetime objects before finding the difference, to take into account the possible changes in the utc offset. If your Python version doesn't support td // timedelta replace it with int(td.total_seconds() // 60). Print((dt2 - dt1) // timedelta(minutes=1)) # minutes The simplest case if both dates use the same utc offset: #!/usr/bin/env python3ĭt1 = datetime.strptime(" 17:31:22", time_format)ĭt2 = datetime.strptime(" 17:31:22", time_format) The result depends on the timezone that corresponds to the input time strings. # They are now in seconds, subtract and then divide by 60 to get minutes. The easiest way to do this is to convert them to Unix timestamps. It would have still given you the same answer since it still returns 2 for days it ignores the hour, min and second from the timedelta.Ī better approach would be to convert the dates to a common format and then do the calculation. In this case it works fine, but if you would have the following. Original problem: from datetime import datetimeĭ2-d1 gives you a datetime.timedelta and when you use days it will only show you the days in the timedelta. ![]() RSabet's answer doesn't work in cases where the dates don't have the same exact time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |