-2

I want to print all the price open high low and close from one date to another, but it only prints the index. Saturdays and Sundays are not present in the csv file. Here is the code: ...

import datetime as dt
import pandas as pd

backtest_start = dt.datetime(2020,1,1)
backtest_end = dt.datetime(2020,1,31)
curr_day = backtest_start
read_data = pd.read_csv("data.csv")
data = pd.DataFrame(read_data)
data['Date'] = pd.to_datetime(read_data['Date'])
for row in data:
    while curr_day < backtest_end:
        print(row)
        break
    curr_day += dt.timedelta(days=1)

... The output I receive is only: Date Open High Low Close

Sameer
  • 17
  • 4
  • can you please provide a sample from the csv? – Geom Jul 07 '21 at 06:25
  • Index of the csv is Date, Open, High, Low, Close and below all the data is listed. – Sameer Jul 07 '21 at 06:29
  • Does this answer your question? [How to select rows in a DataFrame between two values, in Python Pandas?](https://stackoverflow.com/questions/31617845/how-to-select-rows-in-a-dataframe-between-two-values-in-python-pandas) – Geom Jul 07 '21 at 06:34
  • 1
    Does this answer your question? [pandas: filter rows of DataFrame with operator chaining](https://stackoverflow.com/questions/11869910/pandas-filter-rows-of-dataframe-with-operator-chaining) – Amit Gupta Jul 07 '21 at 06:34

2 Answers2

0

You can just filter the dataframe with conditions and print.

print(data[(data['Date']>=backtest_start)&(data['Date']<=backtest_end)])
Suhas Mucherla
  • 1,383
  • 1
  • 5
  • 17
0

You can filter with .between(), as follows:

print(data[data['Date'].between(backtest_start, backtest_end)])
SeaBean
  • 22,547
  • 3
  • 13
  • 25