I have a column with the weekday, another with the month and another with the year. How do I get the actual date in python?
Asked
Active
Viewed 72 times
1
-
Please check [How to Ask](https://stackoverflow.com/help/how-to-ask). It's also best to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to enable others to help you. And check [How to make pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – MagnusO_O Oct 24 '22 at 19:45
-
what did you try? Where is your code? Show example data and expected result. – furas Oct 24 '22 at 22:37
-
if you have `weekday` then you can't get one actual date because this `weekday` can gives 4 or 5 dates in every month. For example `monday, 10, 2022` gives dates `2022.10.03 (monday)`, `2022.10.10 (monday)`, `2022.10.17 (monday)`, `2022.10.24 (monday)`, `2022.10.31 (monday)` – furas Oct 24 '22 at 22:38
3 Answers
2
import pandas as pd
df = pd.DataFrame({"year": [2018], "month": [12], "day": [1]})
df["date"] = pd.to_datetime(df[["year", "month", "day"]]).dt.date
print(df)
# year month day date
# 0 2018 12 1 2018-12-01

Jamie.Sgro
- 821
- 1
- 5
- 17
-
thanks for the help. But I have the day of the week instead of the specific day – andyolivers Oct 24 '22 at 20:49
-
What's the format? Like a string saying "Monday"? or an integer 3 indicating it's a Wednesday? Is there other information provided? – Jamie.Sgro Oct 25 '22 at 01:49
0
You can use the datetime library:
import datetime
x = datetime.datetime(year, month, day)
print(x)
Documentation: https://docs.python.org/3/library/datetime.html

jliu
- 36
- 3
-
hanks for the help. But I have the day of the week instead of the specific day – andyolivers Oct 24 '22 at 20:50