Search code examples
pythonpandasdatetimeindex

Convert a List of Dates as String to PANDAS DatetimeIndex


I have a PANDAS Series of dates (as strings) and frequencies as integers:

date    
2020-02-02  24
2020-02-03  43
2020-02-07  465
2020-02-08  123
2020-02-09  234

But I need them as the output of events in the code below:

all_days = pd.date_range('1/01/2020', periods=300, freq='D')
days = np.random.choice(all_days, 200)
events = pd.Series(np.random.randn(len(days)), index=days)

I mean to keep my data but change the type so that I can use them in another piece of code. Thank you in advance.


Solution

  • Thank you all for helping me. I did like the code below:

    import numpy as np; np.random.seed(sum(map(ord, 'calmap')))
    import pandas as pd
    import calmap
    
    all_days = pd.date_range('1/01/2020', periods=300, freq='D')
    days = np.random.choice(all_days, 200)
    date_index = pd.to_datetime(dates_count.index)
    maximum = max(dates_count.values)
    minimum = min (dates_count.values)
    events = pd.Series(((dates_count.values-minimum)*2/(maximum - minimum))-1, index=date_index)