<jupyter_start><jupyter_text>[Objective](dates-and-times) Work with Dates and Times with Pandas  OverviewPandas has its own datatype datatype that makes it extremely convenient to convert strings that are in standard date formates to datetime objects and then use those datetime objects to either create new features on a dataframe or work with the dataset in a timeseries fashion. This section will demonstrate how to take a column of date strings, convert it to a datetime object and then use the datetime formatting `.dt` to access specific parts of the date (year, month, day) to generate useful columns on a dataframe.  Follow Along  Work with Dates pandas documentation- [to_datetime](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html)- [Time/Date Components](https://pandas.pydata.org/pandas-docs/stable/timeseries.htmltime-date-components) "You can access these properties via the `.dt` accessor" We'll be working with the Loans data (which we will continue to use during the guided project).  To access the Lones dataset, run next code block without changing anything:<jupyter_code>import pandas as pd


loans_data = 'https://raw.githubusercontent.com/LambdaSchool/data-science-practice-datasets/main/unit_1/LendingClub/LoanStats_2018Q4_sm.csv'
loans = pd.read_csv(loans_data)

print(loans.shape)
loans.head()<jupyter_output><empty_output><jupyter_text>For this warm-up assignment, we are going to determine the length of credit history for the individuals in the Loans dataset we just imported.To do this, we are going to need two date values: `issue_d` and `earliest_cr_line`.   Lets look at the string format of the `issue_d` column<jupyter_code>#Print the header of issue_d<jupyter_output><empty_output><jupyter_text>Because this string format %m-%y is a common datetime format, we can just let Pandas detect this format and translate it to the appropriate datetime object.<jupyter_code>#Print the first observation of issue_d using loans['issue_d'][0]<jupyter_output><empty_output><jupyter_text>Now let's apply the pd.to_datetime function to `issue_d`.  Python is very good at figuring out dates.<jupyter_code>#Print issue_d as a datetime value using pd.to_datetime(loans['issue_d'], infer='True')<jupyter_output><empty_output><jupyter_text>What do you notice about how Python handles the day of the month? Assign the values of `issue_d` in the datetime format to a new variable in `loans` called `issue_date`.<jupyter_code>#Create issue_date<jupyter_output><empty_output><jupyter_text>We can use the `.dt` accessor to now grab specific parts of the datetime object. Lets grab just the year from the all of the cells in the `issue_d` column<jupyter_code>#Print the year of issue_date using loans['issue_date'].dt.year



#Print the month using loans['issue_d'].dt.month<jupyter_output><empty_output><jupyter_text>It's just that easy! Now, instead of printing them out, lets add these year and month values as new columns on our dataframe.  Name the year variable `issue_year` and the month variable `issue_month`. Again, you'll have to scroll all the way over to the right in the table to see the new columns.<jupyter_code>#Create issue_year and issue_month.  Print the header of loans and scroll right to see the columns added to the dataset.<jupyter_output><empty_output><jupyter_text>Now let's look at the `earliest_cr_line` column, which is also a string, but that could be converted to datetime format.<jupyter_code>#Print the date of the earliest credit line using loans['earliest_cr_line']<jupyter_output><empty_output><jupyter_text>Convert `earliest_cr_line` to a datetime format using pd.to_datetime and assign the result to the column `earliest_credit`.<jupyter_code>#Created the earliest credit column.<jupyter_output><empty_output><jupyter_text>Determine the length of credit history in days by subtracting `earliest_credit` from `issue_date`.  Save the result as `credit_length_days`.<jupyter_code>#Calculate the length of credit history using (loans['issue_date'] - loans['earliest_credit']).dt.days<jupyter_output><empty_output><jupyter_text>Divide the credit length in days by 365.25 (don't forget leap year!) and save the result as `credit_length_years`.<jupyter_code><jupyter_output><empty_output><jupyter_text>Use the .max() function to determine the longest credit history in years.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07496012759170653, 'fraction_numerical': 0.002734107997265892, 'mean_word_length': 6.304492512479201, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '543097', 'license': 'MIT', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1270, 'n_words': 499, 'path': 'module2-make-features/Date-time warmup.ipynb', 'repo_name': 'BoWarburton/ds11', 'size': '4389'}