0

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.

All help is appreciated; thanks!

  • I find that datetimes usually graph fairly well on x-axis and are easier to modify in terms of duration over floats, and also more clearly demonstrate change over time. (If they have been converted from string [to_datetime](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html)) – Henry Ecker Jul 10 '21 at 03:50
  • 1
    Ahh just graphing the datetime objects works great; thank you!! – Vivrd Prasanna Jul 10 '21 at 04:45

1 Answers1

0

Resolved in comments! Apparently datetime objects graph pretty well on the x-axis.