0

I want to combine a dataframe with datetime. Here is my attempt.

import pandas as pd 
from pandas import DataFrame
import numpy as np

# create df 
df = pd.DataFrame(np.random.uniform(low=10.00, high=32.5, size=(5,2)), columns=['reference', 'target'])

df.head()

reference   target
0   13.559319   24.057471
1   13.101118   19.323373
2   17.295842   18.991576
3   18.123597   13.121553
4   16.328118   28.981584

Then, a timestamp.

import datetime
for a in range(5): # 2 is the number of record
    a = (datetime.date(2010, 1, 15) + datetime.timedelta(a))
    print(a)

2010-01-15
2010-01-16
2010-01-17
2010-01-18
2010-01-19

How to combine both and assign 'date' as the column name for datetime?

2

1 Answer 1

4

Here is an option which avoids the need for the loop using pd.date_range:

df['date'] = pd.date_range(start='2010-01-15', end='2010-01-19')

Or:

df['date'] = pd.date_range(start='2010-01-15', freq='1D', periods=len(df))

Either way, result is:

   reference     target       date
0  21.588889  19.881424 2010-01-15
1  10.042584  31.603628 2010-01-16
2  22.734872  11.061602 2010-01-17
3  13.742643  22.620092 2010-01-18
4  19.675036  19.105152 2010-01-19

For the sake of completeness, here is how your original approach would have worked (creating a list of datetimes and then assigning that to a new column of your DataFrame):

dates = [datetime.date(2010, 1, 15) + datetime.timedelta(i) for i in range(len(df))]
df['date'] = dates

This last option is not recommended though as it is likely slower for large datasets, and also you will find it easier to work with the proper Pandas datetime types if you are storing them in a DataFrame, so stick with pd.date_range for this particular use case. Assigning a list of values to a column can be useful in some other non-datetime related cases though.

Sign up to request clarification or add additional context in comments.

7 Comments

nice, also please consider periods=len(df) for the sake of generalizing :)
Thanks, anky_91, thesilkworm
@k.ko3n thanks for accepting. By any chance did you accidentally misclick the downvote button while accepting? Seems somebody downvoted and not sure why :)
@thesilkworm. sorry, it was me. how to fix it?
@k.ko3n ah I didn't realise that was a rule. Made an edit now which shows how your original approach could have worked (although the other options are still the recommended ones) :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.