以datetime为条件的pandas.isin给出False(Timestamp的绑定方法Timestamp.date?)

mnemlml8  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(87)

我正在使用pandas(以前的一些经验,主要是作为csv解析器和“numpy扩展”)来读取我的测量数据,并想用它创建一个有用的数据结构。不幸的是,我被以下几行卡住了,其中条件总是为false。当检查记录日期与Timestamp.date. dt.date
背景:在板中,我存储哪些威尔斯包含哪些样品。这会不时发生变化。因此,我有一个属性plate.date,它是一个日期列表,然后我的函数将给予样品类型,但仅限于plate.date中给定的日期。因此,我可以创建一些对象板,并根据它对我的数据进行分类。
df具有(除其他外):“记录日期”(实际上是时间点datetime64[ns])、“孔名称”,并应获得“样本类型”、“索引”、“初始测量”。

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)]

siotufzp

siotufzp1#

解由jlandercy给出:
在类定义中使用date()。

相关问题