pandas.DataFrame.last 给出整个数据帧而不是它的一部分(python)

meg*_*xel 4 python dataframe pandas

我在 5 小时数据(每分钟 300 行)的数据帧中使用 history.DataFrame.last('10s) 并给我整个数据帧。

另一方面,命令 history.between_time('22:06:00','22:10:00') 给了我正确的部分。

你可能知道是什么问题吗?谢谢你。

完整的数据框和结果如下:

    history = 

                           time     low    high    open   close      volume
Index                                                                      
2020-04-18 22:14:00  1587248040  170.27  170.32  170.27  170.32   32.788304
2020-04-18 22:13:00  1587247980  170.20  170.27  170.27  170.20    2.495578
2020-04-18 22:12:00  1587247920  170.20  170.27  170.20  170.27   28.454885
2020-04-18 22:11:00  1587247860  170.10  170.21  170.10  170.21   97.663555
2020-04-18 22:10:00  1587247800  169.80  170.10  169.84  169.94  189.118185
                        ...     ...     ...     ...     ...         ...
2020-04-18 17:15:00  1587230100  167.56  167.73  167.56  167.72   15.257272
2020-04-18 17:14:00  1587230040  167.63  167.72  167.67  167.72    0.405094
2020-04-18 17:13:00  1587229980  167.57  167.61  167.57  167.61   23.075999
2020-04-18 17:12:00  1587229920  167.49  167.61  167.61  167.60    9.606858
2020-04-18 17:11:00  1587229860  167.60  167.73  167.60  167.60   53.414672

[300 rows x 6 columns]

history.last('10s')
Out[84]: 
                           time     low    high    open   close      volume
Index                                                                      
2020-04-18 22:14:00  1587248040  170.27  170.32  170.27  170.32   32.788304
2020-04-18 22:13:00  1587247980  170.20  170.27  170.27  170.20    2.495578
2020-04-18 22:12:00  1587247920  170.20  170.27  170.20  170.27   28.454885
2020-04-18 22:11:00  1587247860  170.10  170.21  170.10  170.21   97.663555
2020-04-18 22:10:00  1587247800  169.80  170.10  169.84  169.94  189.118185
                        ...     ...     ...     ...     ...         ...
2020-04-18 17:15:00  1587230100  167.56  167.73  167.56  167.72   15.257272
2020-04-18 17:14:00  1587230040  167.63  167.72  167.67  167.72    0.405094
2020-04-18 17:13:00  1587229980  167.57  167.61  167.57  167.61   23.075999
2020-04-18 17:12:00  1587229920  167.49  167.61  167.61  167.60    9.606858
2020-04-18 17:11:00  1587229860  167.60  167.73  167.60  167.60   53.414672

[300 rows x 6 columns]

history.between_time('22:06:00','22:10:00')
Out[82]: 
                           time     low    high    open   close      volume
Index                                                                      
2020-04-18 22:10:00  1587247800  169.80  170.10  169.84  169.94  189.118185
2020-04-18 22:09:00  1587247740  169.90  169.97  169.90  169.97   11.503376
2020-04-18 22:08:00  1587247680  169.65  169.90  169.65  169.90  103.496717
2020-04-18 22:07:00  1587247620  169.63  169.65  169.65  169.63    2.708217
2020-04-18 22:06:00  1587247560  169.65  169.68  169.68  169.65    6.306080
Run Code Online (Sandbox Code Playgroud)

mcs*_*ner 5

您的索引需要按升序排序。

history.sort_index().last('10s')                                                                                                                                                                                                    
#                            time     low    high    open   close     volume
# Index                                                                     
# 2020-04-18 22:14:00  1587248040  170.27  170.32  170.27  170.32  32.788304
Run Code Online (Sandbox Code Playgroud)

您的索引现在似乎已反转。所以没有sort_index(),最后一行实际上具有最小的索引时间。所有其他时间都之后,同时last()希望删除参考时间之前10 秒以上的内容。