'Get hour of year from a Datetime

Is there a simple way to obtain the hour of the year from a datetime?

dt = datetime(2019, 1, 3, 00, 00, 00) # 03/01/2019 00:00
dt_hour = dt.hour_of_year() # should be something like that

Expected output: dt_hour = 48

It would be nice as well to obtain minutes_of_year and seconds_of_year



Solution 1:[1]

You can use timedelta:

import datetime
dt = datetime.datetime(2019, 1, 3, 00, 00, 00)
dt2 = datetime.datetime(2019, 1, 1, 00, 00, 00)
print((dt-dt2).days*24)

output:

48

Solution 2:[2]

All three functions, reusing their code.

import datetime

def minutes_of_year(dt):
    return seconds_of_year(dt) // 60

def hours_of_year(dt):
    return minutes_of_year(dt) // 60

def seconds_of_year(dt):
    dt0 = datetime.datetime(dt.year, 1, 1, tzinfo=dt.tzinfo)
    delta = dt-dt0
    return int(delta.total_seconds())

Edited to take possible time zone info into account.

Or: subclass datetime, for easier reuse in later projects:

import datetime

class MyDateTime(datetime.datetime):
    def __new__(cls, *args, **kwargs):
        return datetime.datetime.__new__(cls, *args, **kwargs)

    def minutes_of_year(self):
        return self.seconds_of_year() // 60

    def hours_of_year(self):
        return self.minutes_of_year() // 60

    def seconds_of_year(self):
        dt0 = datetime.datetime(self.year, 1, 1, tzinfo=self.tzinfo)
        delta = self-dt0
        return int(delta.total_seconds())

# create and use like a normal datetime object
dt = MyDateTime.now()
# properties and functions of datetime still available, of course.
print(dt.day)
# ... and new methods:
print(dt.hours_of_year())

Solution 3:[3]

You can write a custom function

def get_time_of_year(dt, type = 'hours_of_year'):
  intitial_date = datetime(dt.year, 1,1, 00, 00, 00) 
  duration = dt - intitial_date

  days, seconds = duration.days, duration.seconds
  hours = days * 24 + seconds // 3600
  minutes = (seconds % 3600) // 60

  if type == 'hours_of_year':
    return hours
  if type == 'days_of_year':
    return days
  if type == 'seconds_of_year':
    return seconds
  if type == 'minuts_of_year':
    return minutes

test function

get_time_of_year(dt, 'hours_of_year')
#>>48

Solution 4:[4]

I have the dataframe DF that has the column 'Timestamp' with type datetime64[ns].
The column timestamp looks like this:

DF['Timestamp']:
0      2022-01-01 00:00:00
1      2022-01-01 01:00:00
2      2022-01-01 02:00:00
3      2022-01-01 03:00:00
4      2022-01-01 04:00:00
...       
8755   2022-12-31 19:00:00
8756   2022-12-31 20:00:00
8757   2022-12-31 21:00:00
8758   2022-12-31 22:00:00
8759   2022-12-31 23:00:00
Name: Timestamp, Length: 8760, dtype: datetime64[ns]

I extract 'Hour of Year' in this way:

DF['Year']       = DF['Timestamp'].astype('M8[Y]')
DF['DayOfYear']  = (DF['Timestamp'] - DF['Year']).astype('timedelta64[D]')
DF['Hour']       = DF['Timestamp'].dt.hour + 1 
DF['HourOfYear'] = DF['DayOfYear'] * 24 + DF['Hour']

First it extracts the year from the Timestamp.
Next it creates a time delta from beginning of the year to that Timestamp based on days (in other words, day of year).
Then it extracts the hour from the timestamp.
Finally it calculates the hour of the year with that formula.

And it looks like this in the end:

DF:
               Timestamp  ...  HourOfYear
0    2022-01-01 00:00:00  ...  1.0
1    2022-01-01 01:00:00  ...  2.0
2    2022-01-01 02:00:00  ...  3.0
3    2022-01-01 03:00:00  ...  4.0
4    2022-01-01 04:00:00  ...  5.0
...      
8755 2022-12-31 19:00:00  ...  8756.0
8756 2022-12-31 20:00:00  ...  8757.0
8757 2022-12-31 21:00:00  ...  8758.0
8758 2022-12-31 22:00:00  ...  8759.0
8759 2022-12-31 23:00:00  ...  8760.0
[8760 rows x 6columns]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mehdi Mostafavi
Solution 2
Solution 3
Solution 4 PM0087