datetime
1.Write a Pandas program to extract year, month, day, hour, minute, second and weekday from unidentified flying object (UFO) reporting date.
import pandas as pd
df = pd. read_csv( r'ufo.csv' )
df[ 'Date_time' ] = df[ 'Date_time' ] . astype( 'datetime64[ns]' )
print ( "Original Dataframe:" )
print ( df. head( ) )
print ( "\nYear:" )
print ( df. Date_time. dt. year. head( ) )
print ( "\nMonth:" )
print ( df. Date_time. dt. month. head( ) )
print ( "\nDay:" )
print ( df. Date_time. dt. day. head( ) )
print ( "\nHour:" )
print ( df. Date_time. dt. hour. head( ) )
print ( "\nMinute:" )
print ( df. Date_time. dt. minute. head( ) )
print ( "\nSecond:" )
print ( df. Date_time. dt. second. head( ) )
print ( "\nWeekday:" )
print ( df. Date_time. dt. weekday_name. head( ) )
2. pandas中map, applymap, apply的区别
区别
3.Write a Pandas program to generate sequences of fixed-frequency dates and time spans.
import pandas as pd
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= 'H' )
print ( "Hourly frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= 'min' )
print ( "\nMinutely frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= 'S' )
print ( "\nSecondly frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= '2H' )
print ( "nMultiple Hourly frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= '5min' )
print ( "\nMultiple Minutely frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= 'BQ' )
print ( "\nMultiple Secondly frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= 'w' )
print ( "\nWeekly frequency:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= '2h20min' )
print ( "\nCombine together day and intraday offsets-1:" )
print ( dtr)
dtr = pd. date_range( '2018-01-01' , periods= 12 , freq= '1D10U' )
print ( "\nCombine together day and intraday offsets-2:" )
print ( dtr)