Round Date in Pandas Dataframe [duplicate] - python

This question already has an answer here:
Pandas: convert date in month to the 1st day of next month
(1 answer)
Closed 5 years ago.
I'm attempting to round the date from this dataframe below to the 1st day of the next month - i.e. 1997-10-10 would result in 1997-11-01:
Date
1997-10-10
1997-05-27
1997-04-30
1997-12-19
1997-08-12
Currently my code looks like:
df = pd.read_csv(XXX)
df['Date'] = pd.to_datetime(df.Date)
df['Date'] = df['Date'].dt.date.replace(day = 1)
According to the python library documentation,
date.replace(year=self.year, month=self.month, day=self.day)
Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).
I had presumed that I could use .replace with only 1 argument - day -however I'm receiving a number of errors.

I think you need MonthBegin(0):
df['Date'] = pd.to_datetime(df.Date) + pd.offsets.MonthBegin(0)
print (df)
Date
0 1997-11-01
1 1997-06-01
2 1997-05-01
3 1998-01-01
4 1997-09-01

Related

python get the previous week number as YYYYWW [duplicate]

This question already has answers here:
How can I get the previous week in Python?
(1 answer)
How to get Year-Week format in ISO calendar format?
(2 answers)
Closed last month.
I have a year and week number in an integer format, and I would like to get the previous week number.
I currently have this code:
# get previous week number (as an integer)
weeknumber = 202302
year = weeknumber//100
week = weeknumber-year*100
day = 1
prev_weeknumber = int(''.join(map(str,((datetime.fromisocalendar(year,week,day))- timedelta(days = 7)).isocalendar()[0:2])))
But it provides the following output
prev_weeknumber = 20231
While I need it to have this output
prev_weeknumber = 202301

Python Datetime doing not working properly [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Is there a function to determine which quarter of the year a date is in?
(18 answers)
Closed 1 year ago.
I am trying to def a function which will return the current quarter of the year in a string.
import datetime
date = datetime.datetime(2022,1,1)
month = date.month
year = date.year
if month ==1 or 2 or 3:
quarter = 'Q1'
if month ==4 or 5 or 6:
quarter = 'Q2'
if month ==7 or 8 or 9:
quarter = 'Q3'
if month ==10 or 11 or 12:
quarter = 'Q4'
lookup = quarter +str(year)
print(lookup)
when I run this function I get Q42022. Am I missing something in the if/or statements.
Any help would be greatly appreciated.
You have to explicitly specify
if month==1 or month==2 or month==3:
You could also check inclusion
if month in (1, 2, 3):
if month in {1, 2, 3}:
# and so on

Deriving special period based on date from file - Python

I am new to scripting need some help in writing the code in correct way. I have a csv file in which we have date based on the date I need to create a new column name period which will be combination of year and month.
If the date range is between 1 to 25, month will be the current month from the date
If the date range is greater then 25, month will be next month.
Sample file:
Date
10/21/2021
10/26/2021
01/26/2021
Expected results:
Date
Period (year+month)
10/21/2021
202110
10/26/2021
202111
01/26/2021
202102
Two ways I can think of.
Convert the incoming string into a date object and get the values you need from there. See Converting string into datetime
Use split("/") to split the date string into a list of three values and use those to do your calculations.
Good question.
I've included the code that I wrote to do this, below. The process we will follow is:
Load the data from a csv
Define a function that will calculate the period for each date
Apply the function to our data and store the result as a new column
import pandas as pd
# Step 1
# read in the data from a csv, parsing dates and store the data in a DataFrame
data = pd.read_csv("filepath.csv", parse_dates=["Date"])
# Create day, month and year columns in our DataFrame
data['day'] = data['Date'].dt.day
data['month'] = data['Date'].dt.month
data['year'] = data['Date'].dt.year
# Step 2
# Define a function that will get our periods from a given date
def get_period(date):
day = date.day
month = date.month
year = date.year
if day > 25:
if month == 12: # if december, increment year and change month to jan.
year += 1
month = 1
else:
month += 1
# convert our year and month into strings that we can concatenate easily
year_string = str(year).zfill(4) #
month_string = str(month).zfill(2)
period = str(year_string) + str(month_string) # concat the strings together
return period
# Step 3
# Apply our custom function (get_period) to the DataFrame
data['period'] = data.apply(get_period, axis = 1)

How to calculate number of weeks between two ISO dates [duplicate]

This question already has answers here:
What's the best way to find the inverse of datetime.isocalendar()?
(8 answers)
Closed 2 years ago.
I am reading excel files line by line where I have in one column ISO year_week_start : 2020_53 and in second ISO year_week_end : 2021_01
I am looking how to get distance between these two strings
2021_01 - 2020_53 => 1
2020_40 - 2020_30 => 10
Is this what you are looking for?
from datetime import date
def convert(value):
(year, week) = [int(x) for x in value.split('_')]
result = date.fromisocalendar(year, week, 1)
return result
def distance(date1, date2):
date1 = convert(date1)
date2 = convert(date2)
diff = date1 - date2
weeks = diff.days / 7
return weeks
print(distance('2021_01', '2020_53'))
print(distance('2020_40', '2020_30'))
print(distance('2021_01', '1991_01'))
Output
1.0
10.0
1566.0

Convert integer (YYYYMMDD) to date format (mm/dd/yyyy) in python [duplicate]

This question already has answers here:
How to convert integer into date object python?
(4 answers)
Closed 5 years ago.
I have following dataframe.
id int_date
1 20160228
2 20161231
3 20160618
4 20170123
5 20151124
How to convert above date in int format to date format of mm/dd/yyyy? Want this in particular format for further excel operations?
id int_date
1 02/28/2016
2 12/31/2016
3 06/18/2016
4 01/23/2017
5 11/24/2015
IS it also possible to generate third column with only Month in words? like January, February etc from int_date?
I tried following
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
but date is in datetime object, how to put it as date in pandas DF?
You can use datetime methods.
from datetime import datetime
a = '20160228'
date = datetime.strptime(a, '%Y%m%d').strftime('%m/%d/%Y')
Good Luck;
Build a new column with applymap:
import pandas as pd
dates = [
20160228,
20161231,
20160618,
20170123,
20151124,
]
df = pd.DataFrame(data=list(enumerate(dates, start=1)), columns=['id','int_date'])
df[['str_date']] = df[['int_date']].applymap(str).applymap(lambda s: "{}/{}/{}".format(s[4:6],s[6:], s[0:4]))
print(df)
Emits:
$ python test.py
id int_date str_date
0 1 20160228 02/28/2016
1 2 20161231 12/31/2016
2 3 20160618 06/18/2016
3 4 20170123 01/23/2017
4 5 20151124 11/24/2015
There is bound to be a better solution to this, but since you have zeroes instead of single-digit elements in your date (i.e. 06 instead of 6), why not just convert it to string and convert the subsections?
using datetime would also get you the month strings etc.
//edit:
to be a little more precise, something like this should do the job:
def get_datetime(date):
date_string = str(date)
return datetime.date(date_string[:3], date_string[4:6], date_string[6:8]

Categories