w3resource

Pandas: Create a time-series from a given list of dates as strings

Pandas Time Series: Exercise-6 with Solution

Write a Pandas program to create a time-series from a given list of dates as strings.

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
import datetime
from datetime import datetime, date 
dates = ['2014-08-01','2014-08-02','2014-08-03','2014-08-04']
time_series = pd.Series(np.random.randn(4), dates)
print(time_series)

Sample Output:

2014-08-01    2.025858
2014-08-02    0.307073
2014-08-03   -0.903682
2014-08-04    0.056662
dtype: float64        

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to create a time-series with two index labels and random values. Also print the type of the index.
Next: Write a Pandas program to create a time series object that has time indexed data. Also select the dates of same year and select the dates between certain dates.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}