0 votes
1 view
in Machine Learning by (28.1k points)

I am creating a DataFrame from a csv as follows:

stock = pd.read_csv('data_in/' + filename + '.csv', skipinitialspace=True)

The DataFrame has a date column. Is there a way to create a new DataFrame (or just overwrite the existing one) which only contains rows with date values that fall within a specified date range or between two specified date values?

1 Answer

0 votes
by (32.8k points)

Using DatetimeIndex function:

To select DataFrame value between two dates, you can simply use pandas.date_range function.  

For example:

import numpy as np

import pandas as pd

df = pd.DataFrame(np.random.random((200,3)))

df['date'] = pd.date_range('2000-1-1', periods=200, freq='D')

df = df.set_index(['date'])

print(df.loc['2000-6-1':'2000-6-10'])

yields

Output:

date          0        1        2                          

2000-06-01  0.040457 0.326594  0.492136 

2000-06-02  0.279323 0.877446  0.464523

2000-06-03  0.328068 0.837669  0.608559

2000-06-04  0.107959 0.678297  0.517435

2000-06-05  0.131555 0.418380  0.025725

2000-06-06  0.999961 0.619517  0.206108

2000-06-07  0.129270 0.024533  0.154769

2000-06-08  0.441010 0.741781  0.470402

2000-06-09  0.682101 0.375660  0.009916

2000-06-10  0.754488 0.352293  0.339337

Python’s list indexing, e.g. seq[start : end] includes start but not end index, Pandas df.loc[start_date:end_date] includes both end-points in the result if they are in the index. 

Hope this answer helps.

...