How to resample 1 minute data into 15 minute data?

2024/9/21 5:35:34

CSV file. df before resample and after applying:

df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" ")))
df = df.set_index(pd.DatetimeIndex(df.pop("dateandtime")))
dateandtime Open High Low Close VOLUME
2020-03-11 00:00:00-04:00 2812.75 2813.25 2811.25 2811.25 296
2020-03-11 00:01:00-04:00 2811.25 2811.5 2809.25 2809.5 359
2020-03-11 00:02:00-04:00 2809.25 2810 2808.25 2809.5 189
2020-03-11 00:03:00-04:00 2809.5 2809.5 2806.5 2806.75 602
2020-03-11 00:04:00-04:00 2806.5 2809.75 2806.5 2809 299

How do I resample this 1 minute candlestick data into 15 minute data? I tried:

from dateutil.tz import gettz
import pandas as pd
import finplot as fpltdf = pd.read_csv('/home/user/Documents/finance/fin-smart/lab/ES.csv')
df.rename(columns={'OPEN': 'Open'}, inplace=True)
df.rename(columns={'HIGH': 'High'}, inplace=True)
df.rename(columns={'LOW': 'Low'}, inplace=True)
df.rename(columns={'CLOSE': 'Close'}, inplace=True)
df["dateandtime"] = (pd.to_datetime(df.pop("DATE").str.cat(df.pop("TIME"), sep=" ")))
df = df.set_index(pd.DatetimeIndex(df.pop("dateandtime")))# first day is 2010.01.04
# last day is 2020.03.13
start_date = '2020.03.12'
end_date = '2020.03.13'
df = df.loc[start_date:end_date]df.resample("15T").agg(Open=("Open", "first"),High=("High", "max"),Low=("Low", "min"),Close=("Close", "last"),
)fplt.display_timezone = gettz('America/Chicago')
fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']])
fplt.show()

It gives the same output as without df.resample() and no error or warning. Is df in an incorrect format?

Answer

If you want the most convenient method that creates OHLC only from the Close column:

df.set_index("dateandtime")["Close"].resample("15T").ohlc()

A more detailed method that build OHLC from all 4 existing columns:

df.set_index("dateandtime").resample("15T").agg(Open=("Open", "first"),High=("High", "max"),Low=("Low", "min"),Close=("Close", "last"),
)
https://en.xdnf.cn/q/119825.html

Related Q&A

Python Pygame Lighting for Pong

Hey Guys Im writing a little Pong-Game in Pygame and wanted to use a glowing-effect on the Ball and the Bats. But Pygame dosent support this effects and make solid blocks out of it. Is there a way to h…

python - List.remove method not applicable as the function with the map builtin?

Question Can List.remove not be used as the function to apply in the map(function, iterable, ...) builtin? %%timeit import random m = _max = 10000 n = random.randint(1, m)outer = random.sample(populat…

How do I change a sum for string for it to work?

This is my whole program Im working on (with the function not in the right place due to the code needing to be indented) but anyway there is a problem that Im not sure how to fix.How do I change it so …

Convert type str to numerator/ denominator

Im having some trouble converting type str to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6 and then I import these numbers into python and save them as a list. …

discord.py MemberNotFound exception when passing a real member

When I pass *grant @user add in Discord I get the following exception: Ignoring exception in command grant: Traceback (most recent call last):File "/Users/test/PycharmProjects/slapdash/venv/lib/py…

using argument end= for print function in Python 3.3

Consider the following code:i=0 while i<5:print(i, end=" ")i = i + 1which results in the output:0 1 2 3 4if i want to add a string "the result" before 1 2 3 4, output expected: t…

Get the max value on a list of tuples

I have a list of tuples:card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevan…

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…

Join Operation for Dictionary in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…