0

How would you get the date value that's sitting in the same row as the max value in the hi column?

Max in the hi column is at 9:39 btw

The output of the panda's data frame:

       date         hi       lo      open    close            
12/10/2021 9:24   175.49   175.41   175.42   175.49     
12/10/2021 9:25   175.48   175.25   175.41   175.26    
12/10/2021 9:26    175.3   175.15   175.26   175.18     
12/10/2021 9:27   175.26    175.1   175.16   175.16    
12/10/2021 9:28   175.19    175.1   175.12   175.15    
12/10/2021 9:29   175.27   175.15   175.15   175.23    
12/10/2021 9:30   176.03   175.14   175.25   175.71  
12/10/2021 9:31    175.9   175.46   175.71    175.9   
12/10/2021 9:32    176.1   175.68   175.88   175.73   
12/10/2021 9:33   175.87   175.37   175.74  175.615   
12/10/2021 9:34    176.1   175.52  175.609   176.05   
12/10/2021 9:35   176.11   175.54   176.06   175.64   
12/10/2021 9:36   176.15   175.51   175.65  176.005   
12/10/2021 9:37   176.32   175.87  175.992   176.17   
12/10/2021 9:38   176.53   176.14  176.165   176.47   
12/10/2021 9:39  176.556  176.345   176.48  176.367   
12/10/2021 9:40   176.42  176.005   176.35  176.005   
12/10/2021 9:41   176.05   175.66   176.01   176.01   
12/10/2021 9:42   176.03   175.81  176.011   175.89   
12/10/2021 9:43  176.215   175.88  175.908  176.188   
12/10/2021 9:44   176.45   176.01   176.18  176.025   
12/10/2021 9:45   176.36   175.88   176.02  175.935   
12/10/2021 9:46   176.03   175.76   175.94    175.8   
12/10/2021 9:47  175.775   175.45  175.775   175.56   
12/10/2021 9:48   175.76   175.45   175.55  175.739   
12/10/2021 9:49   175.89   175.56   175.73   175.66   
12/10/2021 9:50   175.86   175.55   175.66   175.83   
12/10/2021 9:51   176.12   175.81   175.84   176.01   
12/10/2021 9:52   176.06  175.721  176.015   175.83   
12/10/2021 9:53   176.01  175.745   175.83   175.78   
12/10/2021 9:54  175.895   175.67   175.79  175.695   
12/10/2021 9:55  175.705   175.24  175.685  175.375   
12/10/2021 9:56   175.38   175.05   175.38  175.155   
12/10/2021 9:57    175.4  174.925   175.15  174.925   
12/10/2021 9:58  175.001   174.69   174.92  174.775   
12/10/2021 9:59   175.21   174.75  174.775   175.18   
12/10/2021 10:00   175.51   175.09   175.18   175.45 

Code below will get the highest high in the Hi column:

df['date'] = pd.to_datetime(df['date'],infer_datetime_format=True)
df = df[(df['date'] >= "12/10/2021 9:30") & (df['date'] <= "12/10/2021 10:00")]['hi'].max()

From this how can I get the Time of when the high of the day occurred?

My desired output >> "HOD Time : 12/10/2021 9:39"

  • Related: [Find row where values for column is maximal in a pandas DataFrame](https://stackoverflow.com/questions/10202570/find-row-where-values-for-column-is-maximal-in-a-pandas-dataframe) – wwii Dec 14 '21 at 00:01
  • Typically to get a single *cell's* value you need to know its row and column. Are you asking how to find the row with the highest value like the link above? Or do you know the row but don't know how to get the value for the column? [Indexing and selecting data](https://pandas.pydata.org/docs/user_guide/indexing.html) – wwii Dec 14 '21 at 00:09
  • Asking how to find the row with the Highest value in the "hi" column so i can get the value in the "date" column on the same row. @wwii – NoLimitLondon Dec 14 '21 at 00:21

1 Answers1

0

Use DataFrame.loc with Series.idxmax:

df.loc[df[(df['date'] >= "12/10/2021 9:30") & (df['date'] <= "12/10/2021 10:00")]['hi'].idxmax()]

Calling idxmax on a column returns the index of the highest value in it, and df.loc[X] returns the row at index X.

  • "TypeError: reduction operation 'argmax' not allowed for this dtype is the call back" Receiving this when putting that code in. @user17242583 – NoLimitLondon Dec 14 '21 at 00:54
  • Odd @Cash. Will you please send the result of `print(df.sample().to_dict())` real quick? –  Dec 14 '21 at 01:15
  • No, I meant for you to execute `print(df.sample().to_dict())` in your Python file and send me the output it produces. –  Dec 14 '21 at 01:37
  • 1
    Output: `{'Date': {479: Timestamp('2021-12-10 13:26:00')}, 'Hi': {479: '177.53'}, 'Lo': {479: '177.425'}, 'Open': {479: '177.47'}, 'Close': {479: '177.47'}, 'Volume': {479: '152169'}}` @user17242583 – NoLimitLondon Dec 14 '21 at 01:48