[Python] Why does to_datetime() distort (squish) the original function shape?

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to programming and wanted to write a small code to generate a set of timestamps with a particular density throughout a year. The idea was to plot the desired density function, chop it up into steps of difference, then cumulative sum them to get the actual points in time (from the start of the year) - nothing fancy.

    Previously I tried to implement a code from chatGPT (I marked the lines that I left in) but basically I had to rewrite most of it manually because it wasn't flexible enough.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt

    # number of data points
    event_num = 50000

    # desired density functions
    lin_up = np.linspace(0, 365, event_num)
    lin_down = lin_up[::-1]
    exp_up = np.exp(-lin_up / 100)[::-1]
    exp_down = np.exp(-lin_up / 100)
    harm_peak = 0.5 * (np.sin((lin_up / 58)-(np.pi/2)) + 1)
    harm_valley = 0.5 * (np.sin((lin_up / 58)+(np.pi/2)) + 1)
    ps_sigm_up = 0.5 * (np.sin((lin_up / 116)-(np.pi/2)) + 1)
    ps_sigm_down = 0.5 * (np.sin((lin_up / 116)+(np.pi/2)) + 1)

    act = harm_peak # so I don't have to manually update the actual function everywhere
    a_magsimum = np.max(act) # normalization 1
    act = act/a_magsimum # normalization 2

    # difference gives the steps (the smaller the difference, the larger the density)
    diff = 1 - act # here changeing the number changes the shape but the baseline too
    diff_magsimum = np.max(diff)

    # cumulative sum gives the actual days
    cum = np.cumsum(diff)
    cum_magsimum = np.max(cum)
    cum = 365 * cum/cum_magsimum

    plt.plot(act)
    plt.title("Desired density function")
    plt.show()

    plt.plot(diff)
    plt.title("Difference")
    plt.show()

    plt.plot(cum)
    plt.title("Cumulative sum")
    plt.show()

    # saving into CSV
    #df = pd.DataFrame({"Date": pd.to_datetime(cum, unit="D", origin="2024-01-01")})
    #df.to_csv("events_new.csv", index=False)

    # dates daily (chatGPT)
    dates = pd.to_datetime(cum, unit="D", origin="2024-01-01").date
    density_per_day = pd.Series(dates).value_counts().sort_index()

    # dates weekly (chatGPT)
    #dates = pd.to_datetime(cum, unit="D", origin="2024-01-01")
    #density_per_week = dates.to_series().dt.to_period("W").value_counts().sort_index()

    # plotting graph - daily breakdown
    plt.plot(density_per_day.index, density_per_day.values, marker='o', linestyle='-')
    plt.xlabel("Date")
    plt.ylabel("Number of events / day")
    plt.title("Eventdensity by time")
    plt.xticks(rotation=45)
    plt.grid(True)
    plt.show()

    # plotting graph - weekly breakdown
    #plt.plot(density_per_week.index.astype(str), density_per_week.values, marker='o', linestyle='-')
    #plt.xlabel("Week")
    #plt.ylabel("Number of events / week")
    #plt.title("Weekly event density")
    #plt.xticks(rotation=45)
    #plt.grid(True)
    #plt.show()


    But the result density function is really different than the original - squished for some reason - and I don't seem to be able to figure out why.

    Desired density function

    Result eventdensity by time

    I observed that if I change the number from 1 to 2 at line 23 it will become a much better looking curve but the baseline will jump from 0 to half maximum (and I don't want that - don't see why that would be necessary).

    I am currently using Anaconda Navigator and Jupyter Notebook but might use VS Code in the foreseeable future.

    Continue reading...

Compartilhe esta Página