-3

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
trincot
  • 317,000
  • 35
  • 244
  • 286
raju
  • 1
  • 1

2 Answers2

0

Two ways I can think of.

  1. Convert the incoming string into a date object and get the values you need from there. See Converting string into datetime
  2. Use split("/") to split the date string into a list of three values and use those to do your calculations.
0

Good question.

I've included the code that I wrote to do this, below. The process we will follow is:

  1. Load the data from a csv
  2. Define a function that will calculate the period for each date
  3. 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)
JonShantz
  • 21
  • 3
  • Glad to hear it. If this answer helped, you can accept it so that the answer will be shared with other users as well. Cheers! – JonShantz Jan 14 '21 at 19:26