I think in pandas is best use to_datetime
with add last value -1
for day of week:
df['datetime'] = pd.to_datetime(df.Year.astype(str) + '-' +
df.Week_No.astype(str) + '-1', format = "%Y-%W-%w")
print(df)
Year Week_No Value datetime
0 2015 52 3 2015 - 12 - 28
1 2016 2 7 2016 - 01 - 11
2 2015 51 5 2015 - 12 - 21
3 2016 1 6 2016 - 01 - 04
4 2015 50 4 2015 - 12 - 14
You can try this:
df['datetime'] = df.apply(lambda x: datetime.datetime.strptime(str(x.Year) + '-' + str(x.Week_No) + '-1', "%Y-%W-%w"), axis = 1)
output:
Year Week_No Value datetime 0 2015 52 3 2015 - 12 - 28 1 2016 2 7 2016 - 01 - 11 2 2015 51 5 2015 - 12 - 21 3 2016 1 6 2016 - 01 - 04 4 2015 50 4 2015 - 12 - 14
How to move ahead to create a datetime anycodings_calendar column?,I think in pandas is best use anycodings_pandas to_datetime with add last value -1 for anycodings_pandas day of week:,I want to track the amount of time we are spending investigating work items on Azure board,A shorter, more compact alternative to loc multiple variables into separate columns in a loop
Sample Data
Year Week_No Value 2015 52 3 2016 2 7 2015 51 5 2016 1 6 2015 50 4
Below is the code that I have tried
import datetime
d = "2015-50"
r = datetime.datetime.strptime(d + '-1', "%Y-%W-%w")
print(r)
2015 - 12 - 14 00: 00: 00
I think in pandas is best use anycodings_pandas to_datetime with add last value -1 for anycodings_pandas day of week:
df['datetime'] = pd.to_datetime(df.Year.astype(str) + '-' +
df.Week_No.astype(str) + '-1', format = "%Y-%W-%w")
print(df)
Year Week_No Value datetime
0 2015 52 3 2015 - 12 - 28
1 2016 2 7 2016 - 01 - 11
2 2015 51 5 2015 - 12 - 21
3 2016 1 6 2016 - 01 - 04
4 2015 50 4 2015 - 12 - 14
You can try this:
df['datetime'] = df.apply(lambda x: datetime.datetime.strptime(str(x.Year) + '-' + str(x.Week_No) + '-1', "%Y-%W-%w"), axis = 1)
output:
Year Week_No Value datetime 0 2015 52 3 2015 - 12 - 28 1 2016 2 7 2016 - 01 - 11 2 2015 51 5 2015 - 12 - 21 3 2016 1 6 2016 - 01 - 04 4 2015 50 4 2015 - 12 - 14
Merging year and week column to create datetime and sorting in python,datetime.datetime.strptime(dateString + '-1', "%Y%W-%w") parameter only looks at one string object. To convert whole series into datetime, you need to do something like this:,Converting a string containing year and week number to datetime in Pandas,How to convert string dataframe column to datetime as format with year and week?
datetime.datetime.strptime(dateString + '-1', "%Y%W-%w") parameter only looks at one string object. To convert whole series into datetime, you need to do something like this:
pd.to_datetime(df['Year_Week'] + '-1', format = "%Y%W-%w")
Initially, the values in datetime are character strings and do not provide any datetime operations (e.g. extract the year, day of the week,…). By applying the to_datetime function, pandas interprets the strings and convert these to datetime (i.e. datetime64[ns, UTC]) objects. In pandas we call these datetime objects similar to datetime.datetime from the standard library as pandas.Timestamp.,More information on the DatetimeIndex and the slicing by using strings is provided in the section on time series indexing.,An overview of the existing date properties is given in the time and date components overview table. More details about the dt accessor to return datetime like properties are explained in a dedicated section on the dt accessor.,By pivoting the data, the datetime information became the index of the table. In general, setting a column as an index can be achieved by the set_index function.
In[1]: import pandas as pd
In[2]: import matplotlib.pyplot as plt
In[3]: air_quality = pd.read_csv("data/air_quality_no2_long.csv")
In[4]: air_quality = air_quality.rename(columns = {
"date.utc": "datetime"
})
In[5]: air_quality.head()
Out[5]:
city country datetime location parameter value unit
0 Paris FR 2019 - 06 - 21 00: 00: 00 + 00: 00 FR04014 no2 20.0 µg / m³
1 Paris FR 2019 - 06 - 20 23: 00: 00 + 00: 00 FR04014 no2 21.8 µg / m³
2 Paris FR 2019 - 06 - 20 22: 00: 00 + 00: 00 FR04014 no2 26.5 µg / m³
3 Paris FR 2019 - 06 - 20 21: 00: 00 + 00: 00 FR04014 no2 24.9 µg / m³
4 Paris FR 2019 - 06 - 20 20: 00: 00 + 00: 00 FR04014 no2 21.4 µg / m³
In[6]: air_quality.city.unique()
Out[6]: array(['Paris', 'Antwerpen', 'London'], dtype = object)
In[7]: air_quality["datetime"] = pd.to_datetime(air_quality["datetime"])
In[8]: air_quality["datetime"]
Out[8]:
0 2019 - 06 - 21 00: 00: 00 + 00: 00
1 2019 - 06 - 20 23: 00: 00 + 00: 00
2 2019 - 06 - 20 22: 00: 00 + 00: 00
3 2019 - 06 - 20 21: 00: 00 + 00: 00
4 2019 - 06 - 20 20: 00: 00 + 00: 00
...
2063 2019 - 05 - 07 06: 00: 00 + 00: 00
2064 2019 - 05 - 07 04: 00: 00 + 00: 00
2065 2019 - 05 - 07 03: 00: 00 + 00: 00
2066 2019 - 05 - 07 02: 00: 00 + 00: 00
2067 2019 - 05 - 07 01: 00: 00 + 00: 00
Name: datetime, Length: 2068, dtype: datetime64[ns, UTC]
pd.read_csv("../data/air_quality_no2_long.csv", parse_dates = ["datetime"])
In[9]: air_quality["datetime"].min(), air_quality["datetime"].max()
Out[9]:
(Timestamp('2019-05-07 01:00:00+0000', tz = 'UTC'),
Timestamp('2019-06-21 00:00:00+0000', tz = 'UTC'))