Comparing date from pandas dataframe to current date

2024/11/17 23:38:29

I'm currently trying to write a script that does a specific action on a certain day. So for example, if today is the 6/30/2019 and in my dataframe there is a 6/30/2019 entry, xyz proceeds to happen. However, I am having troubles comparing the date from a dataframe to a DateTime date. Here's how I created the dataframe

now = datetime.datetime.now()Test1 = pd.read_excel(r"some path")

Heres what the output looks like when I print the dataframe.

  symbol  ...                  phase
0   MDCO  ...                Phase 2
1   FTSV  ...              Phase 1/2
2    NVO  ...                Phase 2
3    PFE  ...  PDUFA priority review
4   ATRA  ...                Phase 1

Heres' how the 'event_date' column prints out

0    05/18/2019
1    06/30/2019
2    06/30/2019
3    06/11/2019
4    06/29/2019

So I've tried a few things I've seen in other threads. I've tried:

if (Test1['event_date'] == now):print('True')

That returns

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I've tried reformatting my the data with:

column_A = datetime.strptime(Test1['event_date'], '%m %d %y').date()

which returns this

TypeError: strptime() argument 1 must be str, not Series

I've tried this

   if any(Test1.loc(Test1['event_date'])) == now:

and that returned this

TypeError: 'Series' objects are mutable, thus they cannot be hashed

I don't know why python is telling me the str is a dataframe, I'm assuming it has something to do with how python exports data from an excel sheet. I'm not sure how to fix this.

I simply want python to check if any of the rows have the same "event_date" value as the current date and return the index or return a boolean to be used in a loop.

Answer
import datetime
import pandas as pdda = str(datetime.datetime.now().date())
# converting the column to datetime, you can check the dtype of the column by doing
# df['event_date'].dtypes
df['event_date'] = pd.to_datetime(df['event_date'])    # generate a df with rows where there is a match
df_co = df.loc[df['event_date'] == da]

I would suggest doing xy or what is required in a column based on the match in the same column. i.e.

df.loc[df['event_date'] == da,'column name'] = df['x'] + df['y']

Easier than looping.

https://en.xdnf.cn/q/118742.html

Related Q&A

How to divide a binary file to 6-byte blocks in C++ or Python with fast speed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Selenium, python dynamic table

Im creating a robot with selenium that get all info from agencies in Brasil, ive alredy done the permutation click between all States and counties, all i have to do nows click in all agencies and get i…

How to split string into column

I got a csv file with some data, and I want to split this data.My column one contains a title, my column 2 contains some dates, and my column 3 contains some text linked to the dates.I want to tran…

remove whitespaces with new line characters

I have a string that looks like that:"\n My name is John\n and I like to go.\n blahblahblah.\n \n\n ".Note - In this string example, there are 5 white-spaces after…

Python tkinter GUI freezing/crashing

from Tkinter import * import tkFileDialog import tkMessageBox import os import ttkimport serial import timeit import time################################################################################…

If/else in python list comprehension

I would like to return random word from file, based on passed argument. But if the argument doesnt match anythning I dont want to return anything. My method looks like:def word_from_score(self,score):p…

Conditional module importing in Python

Im just trying out Maya 2017 and seen that theyve gone over to PySide2, which is great but all of my tools have import PySide or from PySide import ... in them.The obvious solution would be to find/rep…

AttributeError: list object has no attribute lower : clustering

Im trying to do a clustering. Im doing with pandas and sklearn. import pandas import pprint import pandas as pd from sklearn.cluster import KMeans from sklearn.metrics import adjusted_rand_score from s…

I’m dealing with an error when I run server in Django

PS C:\Users\besho\OneDrive\Desktop\DjangoCrushcourse> python manage.py runserver C:\Users\besho\AppData\Local\Programs\Python\Python312\python.exe: cant open file C:\Users\besho\OneDrive\Desktop\Dja…

python threading with global variables

i encountered a problem when write python threading code, that i wrote some workers threading classes, they all import a global file like sharevar.py, i need a variable like regdevid to keep tracking t…