ok I am getting the attribute Error:enter with statement. trying to have python check for next Mondays date and if there is that date move forward then if there is change it to Tuesday then if there is not print out error: there is no starting next week... but getting that error though and cant figure it out.
import os
import pandas as pd
import datetime as DT
from dateutil.relativedelta import relativedelta, MO, TU
hr_file = "Upcoming Hires.xlsx"
with pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"]) as reader:
now = now = DT.datetime.now()
nm = now - relativedelta(weekday=MO( 1))
next_monday = now - relativedelta(weekday=MO( 1))
if next_monday in reader:
print("true")
if not next_monday in reader:
nm = now - relativedelta(weekday=TU( 2))
next_monday = nm.strftime("%#m/%#d/%#y")
if next_monday in reader:
print("had to move the date for the start date to tuesday but it True")
if not next_monday in reader:
print("error: there is no starter next week...")
getting this for the error
AttributeError Traceback (most recent call last)
c:\masterscript\test.py in <cell line: 1>()
----> 7 with pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"]) as reader:
8 now = now = DT.datetime.now()
9 nm = now - relativedelta(weekday=MO( 1))
AttributeError: __enter__
CodePudding user response:
You don't need a context manager to use pd.read_excel
since this functions returns a DataFrame or dict of DataFrames, from documentation:
Returns: DataFrame or dict of DataFrames
DataFrame from the passed in Excel file. See notes in sheet_name argument for more information on when a dict of DataFrames is returned.
import pandas as pd
import numpy as np
with pd.DataFrame(np.random.rand(5,10)) as reader:
print(reader)
AttributeError Traceback (most recent call last)
----> 1 with pd.DataFrame(np.random.rand(5,10)) as reader:
2 print(reader)
AttributeError: __enter__
You can just assign the return of pd.read_excel
to a variable and adjust the reader
to the column containing the dates you need. Something similar to the following:
...
hr_file = "Upcoming Hires.xlsx"
df = pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"])
now = now = DT.datetime.now()
nm = now - relativedelta(weekday=MO( 1))
next_monday = now - relativedelta(weekday=MO( 1))
if next_monday in df['Start Date']:
print("true")
if not next_monday in df['Start Date']:
...
...