How to Create a Time Series Dataframe in Python

To create a time series dataframe in Python, you can use the date_range() function from the pandas library.

The following example shows how to create a time series dataframe in Python using the date_range() function.

Using date_range() Function

We can use the date_range() function to create a time series dataframe.

Suppose we have the following data:

# Import pandas library
import pandas as pd

# Create time series data
dates = pd.date_range(start='2022-01-01', periods=6, freq='D')

# Create dataframe
df = pd.DataFrame({
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
}, index=dates)

# Print updated dataframe
print(df)

Output: 👇️

           Product_Code Product_Name  Price  Status
2022-01-01        A-101       Laptop   4500       1
2022-01-02        A-102       Mobile    550       1
2022-01-03        A-103      Printer    250       1
2022-01-04        B-101     Keyboard     50       0
2022-01-05        B-102      Scanner    350       1
2022-01-06        B-104        Mouse     50       1

In this example, we use the date_range() function to create a range of dates starting from ‘2022-01-01’ for 6 periods with a daily frequency.

We then create a dataframe df with the time series data as the index.

The output shows the dataframe with time series data.

Conclusion

We can use the date_range() function from the pandas library to create a time series dataframe in Python.