Advertisement
elena1234

date format in Python

Oct 14th, 2022 (edited)
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | Source Code | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. from dateutil import parser
  4.  
  5.  
  6. df = pd.read_csv('movies_complete.csv', parse_dates = ['release_date'])
  7.  
  8. ######################################################
  9. all_ads_until_10_Oct['Date'] = [parser.parse(x) for x in all_ads_until_10_Oct['Date']]
  10. all_ads_until_10_Oct.head()
  11.  
  12. pd.to_datetime(cars['model_year'], format= '%Y')
  13.  
  14. cars['model_year'].astype('datetime64[ns]')
  15.  
  16. #####################################################
  17. # convert year (string) into full date
  18. df_auto = df_auto.replace(r'^\s*$', np.nan, regex=True)
  19. df_auto = df_auto.dropna(subset = ["Year Manifacture"])
  20. df_auto['Year Manifacture'] = df_auto['Year Manifacture'].str.extract(r'(\d+)').astype(int)
  21. df_auto["Year Manifacture1"] = df_auto["Year Manifacture"].apply(lambda row: f'12/31/{row}')
  22. # and after that you can parse
  23. df_auto["Year Manifacture_new"] = [parser.parse(x) for x in df_auto["Year Manifacture_new"]]
  24.  
  25. #####################################################
  26. # How to extract the months from date
  27. month_quantity_2017 = df_category_date_quantity.loc[mask1 & mask2].groupby(df_category_date_quantity.TransactionDate.dt.month).agg({'Quantity': 'sum'}).reset_index()
  28. month_quantity_2017
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement