检查非索引列是否在Pandas中排序 [英] Check whether non-index column sorted in Pandas

查看:75
本文介绍了检查非索引列是否在Pandas中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以测试数据框是否由给定的非索引列排序(即非索引列是否与is_monotonic()等效),而无需再次调用排序,也无需转换列成索引?

Is there a way to test whether a dataframe is sorted by a given column that's not an index (i.e. is there an equivalent to is_monotonic() for non-index columns) without calling a sort all over again, and without converting a column into an index?

推荐答案

pd.algos中有一些可能有用的功能.它们都是未记录的实现细节,因此它们可能因发行版本而异:

There are a handful of functions in pd.algos which might be of use. They're all undocumented implementation details, so they might change from release to release:

>>> pd.algos.is[TAB]
pd.algos.is_lexsorted          pd.algos.is_monotonic_float64  pd.algos.is_monotonic_object
pd.algos.is_monotonic_bool     pd.algos.is_monotonic_int32
pd.algos.is_monotonic_float32  pd.algos.is_monotonic_int64    

is_monotonic_*函数采用指定dtype的数组和在大多数情况下应为False的类似时间的"布尔值. (对于涉及以整数表示的时间的情况,Pandas将其设置为True.)返回值是一个元组,其第一个元素表示数组是否单调不递减,其第二个元素表示数组是否单调不递增. .其他元组元素取决于版本:

The is_monotonic_* functions take an array of the specified dtype and a "timelike" boolean that should be False for most use cases. (Pandas sets it to True for a case involving times represented as integers.) The return value is a tuple whose first element represents whether the array is monotonically non-decreasing, and whose second element represents whether the array is monotonically non-increasing. Other tuple elements are version-dependent:

>>> df = pd.DataFrame({"A": [1,2,2], "B": [2,3,1]})
>>> pd.algos.is_monotonic_int64(df.A.values, False)[0]
True
>>> pd.algos.is_monotonic_int64(df.B.values, False)[0]
False

所有这些函数都假定特定的输入dtype,甚至是is_lexsorted,它假定输入是int64数组的列表.将其传递给错误的dtype,就会感到非常困惑:

All these functions assume a specific input dtype, even is_lexsorted, which assumes the input is a list of int64 arrays. Pass it the wrong dtype, and it gets really confused:

In [32]: pandas.algos.is_lexsorted([np.array([-2, -1], dtype=np.int64)])
Out[32]: True
In [33]: pandas.algos.is_lexsorted([np.array([-2, -1], dtype=float)])
Out[33]: False
In [34]: pandas.algos.is_lexsorted([np.array([-1, -2, 0], dtype=float)])
Out[34]: True

我不完全确定为什么Series没有某种短路is_sorted.可能有些事情使它变得比看起来复杂.

I'm not entirely sure why Series don't already have some kind of short-circuiting is_sorted. There might be something which makes it trickier than it seems.

这篇关于检查非索引列是否在Pandas中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆