'Pandas: groupyby with 'nth' sorting even though sort assigned to FALSE

GIVEN

A database df (like below).

date         time    open    high    low     close
18-10-2010    9:15   6066    6079    6050    6052
18-10-2010    9:25   6050    6053    6034    6053
.
.
18-10-2010    15:25  6123    6136    6123    6131.5
19-10-2010    9:15   6108    6109    6094    6096.75
.
.
03-03-2022    15:25  16524   16528   16518   16528

MY GOAL

I want to extract the highest high, lowest low and close of the day for everyday. I have the following code for that.

day_HLC = df.groupby('day', sort=False).high.max()
day_HLC = day_HLC.reset_index()
day_HLC['low'] = df.groupby('day', sort=False).low.min().reset_index().low
test = df.groupby('day', sort=False).close.nth(-1).reset_index()

MY PROBLEM

The highest high and the lowest low are working out perfectly in the right order, without any sorting, as required. But the code for close of the day is giving me a sorted pandas series that is creating problems for further calculations.

highest high and lowest low working as I want:

             day      high       low
0     18-10-2010   6136.00   6007.60
1     19-10-2010   6166.40   6024.10

test failing and giving me a sorted result

             day     close
0     01-01-2013   6007.00
1     01-01-2014   6360.65
2     01-01-2015   8347.05
3     01-01-2016   7983.00
4     01-01-2018  10490.00

QUESTION

How can I get a non sorted result for the close of everyday without it being sorted?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source