pythondatetimepython-datetimedatetime-conversion

Convert Python datetime or date to float for representing an x-axis


I'm dealing with a large dataset that has dates in it (in the format "mm/dd/yyyy") and I'm trying to graph a certain metric in relation to that dates. Now since the dates need to be in the x-axis, I know I need to convert them into floats. I currently have two approaches:

  1. Use string parsing to get month/year/dates, convert them to floats, and add them together like so:
def get_date_float(date_str):
    # date_str = something like "01/13/2021"
    m, d, y = float(date_str[:2]), float(date_str[3:5]), float(date_str[6:])
    return y + (m / 12) + (0.01 * d / num_days_in_month(int(m)))

Note: num_days_in_month uses the monthrange function from the calendar module. However, this results in not-so-accurate floats for the dates. For example, the date "12/15/2020 " becomes the float "2021.004839" which is clearly wrong. I'm not sure why this is happening - I tried different operations on the m/d/y variables from above but there's always some degree of error.

  1. After reading this answer, I tried using the datetimemodule to convert the string into a float like so:
def date_str_to_datetime_to_float(date_str):
    # date_str = "12/15/2020"
    datetime_obj = datetime.strptime(date_str, "%m/%d/%Y")
    print(datetime_obj.timestamp())

However, the timestamp is a very large number (# of milliseconds since some starting time?). For example, the date "12/15/2020" becomes the timestamp "1608019200.0". However, I want something like "2020.921" or something like that.


Solution

  • datetime objects graph pretty well on the x-axis.