Fnark Man
Fnark Man

Reputation: 133

How to accurately implement typing with pandas?

So I've got a class, and I've defined

class Example():
    self.database: pd.DataFrame = pd.DataFrame(
        columns = ["Example", "Time Example" : "datetime64[ns]"]
    ).astype({"Example": float})

Now, when I try to do operations such as

    def example_time_method(self, id: str, date: dt.datetime):
        elapsed = date - self.database.loc[id, "Time Example"]

    def example_float_method(self, id: str, num: float):
        diff = num - self.database.loc[id, "Example"]

I get the errors:

Now, for some reason, if I change it to loc[id]["Example"] instead of loc[id, "Example"], this works for the float example. However, for the datetime example I still get an error:

I'm really confused as to what's going on, any help would be appreciated!

I've tried adding typing, doing conversions (e.g. float(...)) but the errors always go further in. Obviously I've tried the above of changing the [a, b] to [a][b] but that just produces a different error. And even when it works for the float case, I want to know why it works!

Upvotes: 1

Views: 79

Answers (0)

Related Questions