为什么isinstance仅在系列映射中返回错误的值?

时间:2018-05-14 15:21:59

标签: python pandas

对isinstance的调用会返回True,但在一个系列中的地图内部返回False(以及数据框上的applymap)...

s.map(lambda x: x.isoformat() if isinstance(x, pd.Timestamp) else x).iloc[0]
Timestamp('2018-05-11 06:00:00+0000', tz='UTC')

对此系列中单个值的isinstance调用会产生True。

  <script>
    if (window.location.href.indexOf(".com/courses") >= 0) {


        $(document).ready(function () {



            jQuery.noConflict();



            $(".atr458").hover(function () {
                $(".atr458").css("color", "#09A59A");

            });

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(7) > div > div.course-listing-extra-info.col-xs-12"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );


            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(1) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(3) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a href='' class='ahre55' >Mas Informacion</a>"
            );


            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(5) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(6) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );


            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(8) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(9) > div > div.course-listing-extra-info.col-xs-12 > div:nth-child(3) > div"
            ).html(
                "<a class='ahre55' href=''>Mas Informacion</a>"
            );


            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(2) > div > div.course-listing-extra-info.col-xs-12"
            ).append(
                "<a class='atr458' id='atr457' href=''>Aplicar Aquí</a>" //COMPELTAR CON LINK 
            );
            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(10) > div > div.course-listing-extra-info.col-xs-12"
            ).append(
                "<a class='atr458' id='atr457' href=''>Aplicar Aquí</a>" //COMPELTAR CON LINK
            );

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(11) > div > div.course-listing-extra-info.col-xs-12"
            ).append(
                "<a class='atr458' id='atr457' href=''>Aplicar Aquí</a>" //COMPELTAR CON LINK
            );

            $(
                "body > div.view-school > div > div > div.row.course-list.list > div:nth-child(12) > div > div.course-listing-extra-info.col-xs-12"
            ).append(
                "<a class='atr458' id='atr457' href=''>Aplicar Aquí</a>" //COMPELTAR CON LINK
            );
            $(".atr458").css("left", "30%");
        })
    }


    jQuery.noConflict();

</script>

在系列的地图上,它给出了True。

{{1}}

但如果我们根据该值尝试某些事情,请说转换为字符串......

{{1}}

...它似乎返回了False并且没有调用方法isoformat(实际的方法调用是无关紧要的,因为它没有被调用)。

2 个答案:

答案 0 :(得分:2)

查看.map的{​​{3}},看来Pandas正在检查系列的类型是否为source。正如OP指出的那样,对于不同的时区,这将表现不同。让

s1 = pd.Series([
    pd.Timestamp(2018,5,11,6,0,0,0),
])

s2 = pd.Series([
    pd.Timestamp(2018,5,11,6,0,0,0, pytz.timezone('UTC')),
])

调用.map时,会检查pd.api.types.is_extension_type(s)。如果s == s1,则返回False,而如果s == s2则返回True

因此,s2.map变为s2._values.map。由于s2._values的类型为DatetimeIndex,因此调用了.map的相关实现。如果发生错误,它首先会尝试调用f(s2._values)并恢复为s2._values.map(f)

在这种情况下,f = lambda x: x.isoformat(x) if isinstance(x, pd.Timestamp) else x。没有错误发生,因为f检查isinstance(s2._values, pd.Timestamp)是否失败。因此,f(s2._values)会返回s2._values。实际上,这可以通过s2._values is f(s2._values) == True进行验证。

一种解决方法是确保未调用pd.api.types.is_extension_type,例如s.astype(object).map

答案 1 :(得分:1)

看起来将datetime系列转换为DatetimeIndex,然后将索引传递给该函数。当然,索引未通过isinstance检查。

def f(x):
    print(x)
    if isinstance(x, pd.Timestamp):
        print('{} == {}'.format(type(x).__name__, pd.Timestamp.__name__))
        return x.isoformat()
    else:
        print('{} != {}'.format(type(x).__name__, pd.Timestamp.__name__))
        return x

print(s.map(f))

输出:

DatetimeIndex(['2018-05-11 06:00:00+00:00'], dtype='datetime64[ns, UTC]', freq=None)
DatetimeIndex != Timestamp
0   2018-05-11 06:00:00+00:00
dtype: datetime64[ns, UTC]

所有系列都不会发生这种情况,但似乎取决于类型。 Myabe它适用于扩展类型或仅适用于datetime。