AttributeError in sample('H').ohlc()

Asked 2 years ago, Updated 2 years ago, 62 views

I wanted to resample in OHLC format on Python, so I wrote the following code, but it turned out to be AttributeError.
Does anyone know what's wrong?

The running environment is PythonScript in AzureML.

Source:

import pandas as pd

dataframe1['Time'] = pd.to_datetime(dataaframe1['Time'], unit='s')
dataframe1.index=dataaframe1 ['Time']
x = dataframe1.resample('H').ohlc()

dataframe1:

Time Open High Low Close 
20170102 020100  116.875  116.915  116.875  116.901 
20170102 020200  116.901  116.901  116.901  116.901 
20170102 020300  116.901  116.906  116.897  116.900 

error messages:

Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ---------
Caught exception while executing function: Traceback (most recent call last):
  File "C:\server\invokepy.py", line 199, in batch
    odfs=mod.azureml_main(*idfs)
  File "C:\temp\16dad51ce7994c25aa02a0a388e26709.py", line 44, in azureml_main
    x = dataframe1.resample('H').ohlc()
  File "C:\pyhome\lib\site-packages\pandas\core\generic.py", line 1843, in__getattr__
    (type(self).__name__,name))
AttributeError: 'DataFrame' object has no attribute 'ohlc'
Process returned with non-zero exit code 1

---------- End of error message from Python interpreter ---------

That's all.
Thank you for your cooperation.

7/17 Add

Thank you for your reply, magichan.
As you said, it's already OHLC format...
The explanation of the purpose was incorrect.The correct answer is "I want to resample data in OHLC format."
I tried the source you told me as follows, but I got a similar error.

import pandas as pd

dataframe1['Time'] = pd.to_datetime(dataaframe1['Time'], unit='s')
dataframe1.index=dataaframe1 ['Time']
x = dataframe1.resample('H').ohlc()
Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ---------
Caught exception while executing function: Traceback (most recent call last):
  File "C:\server\invokepy.py", line 199, in batch
    odfs=mod.azureml_main(*idfs)
  File "C:\temp\4cf09401e1994ec1a2112e2d81ef4ff3.py", line 49, in azureml_main
    x = dataframe1.resample('H') .agg({
  File "C:\pyhome\lib\site-packages\pandas\core\generic.py", line 1843, in__getattr__
    (type(self).__name__,name))
AttributeError: 'DataFrame' object has no attribute 'agg'
Process returned with non-zero exit code 1

Maybe the execution environment is worse than grammar...

python pandas azure

2022-09-30 15:44

1 Answers

dataframe1 looks like it's already in OHLC format...

If you already want to Resample data in OHLC format,

 x = dataframe1.resample('H') .agg({
    'Open': 'first',
    'High': 'max',
    'Low': 'min',
    'Close': 'last'})

I think it will be like this

[Additional]
I will list the operation sample code that works as it is.
See how it works.

import pandas as pd
importio

data=""
Time, Open, High, Low, Close
20170102 020100,116.875,116.915,116.875,116.901
20170102 020200,116.901,116.901,116.901,116.901
20170102 020300,116.901,116.906,116.897,116.900
"""

dataframe1=pd.read_csv(io.StringIO(data), parse_dates=['Time'], index_col='Time')
res = dataframe1.resample('H') .agg({
    'Open': 'first',
    'High': 'max',
    'Low': 'min',
    'Close': 'last'})

print(res)
#                         Low High Open Close
#Time
#2017-01-02 02:00:00  116.875  116.915  116.875  116.9


2022-09-30 15:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.