以日期时间为条件的pandas.isin给出False(时间戳的绑定方法Timestamp.date???)

问题描述 投票:0回答:1

我正在使用 pandas(之前有过一些经验,主要是作为 csv 解析器和“numpy 扩展”)来读取我的测量数据,并希望用它创建一个有用的数据结构。不幸的是,我被困在以下几行中,其中条件始终为假。根据plate.date 检查记录日期时似乎存在(类型?)问题。 df...dt.date 似乎是“时间戳的绑定方法 Timestamp.date”,我没有找到有关此的可理解的信息。

背景:在板中,我存储哪些孔包含哪些样品。这会不时发生变化。所以我有一个属性plate.date,它是日期列表,然后我的函数将返回样本类型,但仅针对plate.date中给出的日期。所以我可以创建一些对象板并根据它对我的数据进行分类。

df 具有(除其他外):“记录日期”(实际上是时间点 datetime64[ns])、“井名称”并应获得“sample_type”、“index”、“initial_measurement”。

    if hasattr(plate, "date"):
        condition = df["Record Date"].dt.date.isin(plate.date)
    else:
        condition = df["Well Name"] != None # True for available data
    
    df.loc[condition, ["sample_type", "index", "initial_measurement"]] = list((df.loc[condition, "Well Name"].astype(str).apply(get_sample_info)))

    
    # Change the data types of the new columns
    df = df.astype({"sample_type": str, "index": pd.Int64Dtype(), "initial_measurement": bool})

    # Helper function to get the sample type, index, and measurement for a given well.
    def get_sample_info(well):
        for sample_type, well_list in plate.__dict__.items():
            
            if well in well_list and sample_type.replace("_second", "") in plate.well_ranges:
                initial_measurement = True if "_second" not in sample_type else False
                sample_type = sample_type.replace("_second", "")
                index = well_list.index(well) + 1
                return sample_type, int(index), initial_measurement
        return None, np.nan, None
class Plate:
    def __init__(self, ..., date=None):

   ...

        if date is not None:
            if isinstance(date, str):
                # fine
                self.date = list(parse(date).date)
            elif isinstance(date, list) or isinstance(date, tuple):
                # fine, but check type of items
                if all((isinstance(item, str) or isinstance(item, datetime)) for item in date):
                    self.date = [parse(item).date for item in date]
                else:
                    raise TypeError("The data type of the elements in the date list/tuple must be datetime or strings.")
            elif isinstance(date, datetime):
                # fine
                self.date = date.date
            else:
                raise TypeError("The data type of parameter date must be datetime.date, string (containing date) or list/tuple (of dates/strings).")

我尝试使用以下代码打印结果和数据类型:

datetime.datetime.fromtimestamp(relevant_facs_measurements['Record Date'].iloc[0].date)
TypeError: 'method' object cannot be interpreted as an integer

parse("2023-12-01").date.isin(plate.date)
AttributeError: 'builtin_function_or_method' object has no attribute 'isin'


df['Record Date'].iloc[0].date == parse("2023-12-01")
False

type(df['Record Date'].iloc[0].date))
<bound method Timestamp.date of Timestamp('2023-12-01 17:16:00')>

type(parse("2023-12-01"))
datetime.datetime

df['Record Date'].dt.date.isin((parse("2023-12-01"),parse("2023-12-06")))

在我的代码的另一部分,它工作完美:

relevant_df=df[df["Record Date"].dt.date.isin(dates_to_keep)]
dates_to_keep = [datetime.date(2023,12,1), datetime.date(2023,12,6)]
python pandas datetime python-dateutil
1个回答
0
投票

解决方案由jlandercy给出:

在类定义中使用 date()。

© www.soinside.com 2019 - 2024. All rights reserved.