Reputation: 337
I have the next date:
0 2019-06-29
Name: end date, dtype: datetime64[ns]
This is a part of the dataframe named data:
data
PointAlias start Date end date
0 AO122732 2018-06-29 2019-06-29
then I transform it with the next code:
end_date_transformed = []
for x in range(len(data['end date'])):
transf = pd.to_datetime(data['end date'][x]).date()
end_date_transformed.append(transf)
end_date_transformed
output: [datetime.date(2019, 6, 29)]
I want to know if it is possible to transform it from a list to just a string to apply datetime.datetime.strftime(end_date_transformed, %Y%m%d')
.
Is there a way of doing it?
The final objective of this is to be able to filter a dataframe of dates to just get those dates that are <= end_date_transformed
. Something like:
df
date
0 2018-06-29
1 2018-10-29
2 2019-02-28
3 2019-06-29
4 2019-10-29
5 2020-02-29
df[df['date'] <= end_date_transformed] # This does not work, the output is:
ValueError: Arrays were different lengths: 44 vs 1
I understand that the code above will not work, but if I do a datetime.date(year, month, day) and use it like end_date_transformed
it works so the problem is in the transformation of the end date.
If someone can help, I will be grateful.
Thank you so much for taking your time to read it and answer.
Upvotes: 0
Views: 2419
Reputation: 9019
This should work for you for a single end date:
import pandas as pd
from datetime import datetime
data = pd.DataFrame([['A0122732','2018-06-29','2019-06-29']], columns=['PointAlias','start date','end date'])
df = pd.DataFrame([
['2018-06-29'],
['2018-10-29'],
['2019-02-28'],
['2019-06-29'],
['2019-10-29'],
['2020-02-29']],
columns=['date'])
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
end_date = datetime.strptime(data['end date'].values[0], '%Y-%m-%d')
df[df['date'] <= end_date]
Returns:
date
0 2018-06-29
1 2018-10-29
2 2019-02-28
3 2019-06-29
Upvotes: 1