简体   繁体   English

pandas.DataFrame.loc:尝试根据日期时间修改列的值时返回类型错误

[英]pandas.DataFrame.loc: TypeError returned when trying to modify values of column based on datetime

I have a data frame that contains a column of dates and another column that I'd like to modify according to the date.我有一个数据框,其中包含一列日期和另一列,我想根据日期进行修改。 However when I try to do this using the.loc method, I get但是,当我尝试使用 .loc 方法执行此操作时,我得到

TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

Could anyone please explain 1) why this error comes up - the dates are datetime objects, and 2) how I can modify the second column.谁能解释一下1)为什么会出现这个错误-日期是日期时间对象,以及2)我如何修改第二列。 I include a MWE below.我在下面包括了一个 MWE。

Many thanks非常感谢

from datetime import datetime as DT

import numpy as np
import pandas as pd

def random_dates(start, end, n, unit='D', seed=None):
    if not seed:  # from piR's answer
        np.random.seed(0)

    ndays = (end - start).days + 1
    return pd.to_timedelta(np.random.rand(n) * ndays, unit=unit) + start


start_date = pd.to_datetime('2010-01-01')
end_date   = pd.to_datetime('2020-01-01')
df = pd.DataFrame(columns=['Date', 'Names'])

N = 10
df['Date'] = random_dates(start_date, end_date, N)
df = df.assign(Names = ['A'] * N)

df.loc['Date' < DT(2015, 1, 1), 'Names'] = 'B'

(random_dates function from this post ) (来自这篇文章的随机日期 function )

Switch the line换线

df.loc['Date' < DT(2015, 1, 1), 'Names'] = 'B'

to

df.loc[df.Date < DT(2015, 1, 1), 'Names'] = 'B'

This would solve it.这将解决它。

You are not using the df.loc statement properly.您没有正确使用df.loc语句。 For your particular logic, you want to change the values of column Names to "B" when a row's corresponding date is < 2015-1-1 .对于您的特定逻辑,当行的对应日期为 < 2015-1-1时,您希望将列Names的值更改为“B”。 When you want to include any conditions in df.loc , the proper way to use it is like this:当您想在df.loc中包含任何条件时,使用它的正确方法是这样的:

df.loc[(df['Date'] < DT(2015, 1, 1)), 'Names'] = 'B'

For a detailed guide on how to use conditions with df.loc , you can refer this link有关如何使用df.loc条件的详细指南,您可以参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM