小弟我在篩選Dataframe中的資料困擾住了,想請問一下各位大神,以下是我的code以及資料:
code:
def search_time_FranklinWc(o):
conn = pymysql.connect(**db_settings)
command = f"""
SELECT * FROM Franklin_topic_word_cloud where DATEDIFF(NOW(), start_date) < 14
"""
df = pd.read_sql(command, conn)
df['start_date'] = df['start_date'].apply(str)
df['end_date'] = df['end_date'].apply(str)
df['start_week'] = df['start_date'].apply(lambda x :datetime.datetime.strptime(x, "%Y/%m/%d").isocalendar()[1])
df['end_week'] = df['end_date'].apply(lambda x :datetime.datetime.strptime(x, "%Y/%m/%d").isocalendar()[1])
if o == "week":
result = df[df['start_week']!=df['end_week']]
elif o == "day":
result = df[df['start_week']==df['end_week']]
return result
資料:
我希望能只抓end_date為2021/9/7的資料,所以我將code改為以下:
def search_time_FranklinWc(o):
conn = pymysql.connect(**db_settings)
command = f"""
SELECT * FROM Franklin_topic_word_cloud where DATEDIFF(NOW(), start_date) < 14
"""
today = datetime.date.today()
target_day = str(today- datetime.timedelta(days=2))
df = pd.read_sql(command, conn)
df['start_date'] = df['start_date'].apply(str)
df['end_date'] = df['end_date'].apply(str)
df['start_week'] = df['start_date'].apply(lambda x :datetime.datetime.strptime(x, "%Y/%m/%d").isocalendar()[1])
df['end_week'] = df['end_date'].apply(lambda x :datetime.datetime.strptime(x, "%Y/%m/%d").isocalendar()[1])
if o == "week":
target = df[df['start_week']!=df['end_week']]
result = df[df['end_date']=target_day]
elif o == "day":
result = df[df['start_week']==df['end_week']]
return result
但它未如期完成,後來我發現可能是因為target_day='2021-9-7',所以並未匹配到。
我該如何正確處理時間格式,或是有沒有更好的篩選方式,請求各位大神解答qqq
我的話可能會先把他轉成同一個格式再來處理
不過會有點笨..
就一個一個看有什麼格式再慢慢加上去
依你現況我會寫成類似下面這樣
import datetime
dat1 = '2021-9-7'
dat2 = '2021-08-31'
format = "%Y-%M-%d"
format2 = "%Y/%M/%d"
def get_same_dat_string(date_string):
dat = None
try:
dat = datetime.datetime.strptime(date_string, format2)#預設是%Y/%M/%d
except ValueError:
try:
dat = datetime.datetime.strptime(date_string, format).strftime(format2)#是%Y-%M-%d轉回預設
except ValueError:
print(date_string)#超乎預期的格式就印出來
return dat
print(get_same_dat_string(dat1))
print(get_same_dat_string(dat2))