pandas中关于时间的处理总结

1.Transform the Date column as a datetime type

apple.Date = pd.to_datetime(apple['Date'])
apple.dtypes

2.Set the date as the index

apple = apple.set_index('Date')
apple.head()

3.Is there any duplicate dates?

apple.index.is_unique

4. Get the last business day of each month

apple_month = apple.resample('BM').mean()
apple_month.head()

5.What is the difference in days between the first day and the oldest

(apple.index.max() - apple.index.min()).days

6.How many months in the data we have?

apple_months = apple.resample('BM').mean()
len(apple_months.index)

7.Create your time range (start and end variables). The start date should be 01/01/2015 and the end should today (whatever your today is)

start = dt.datetime(2015, 1, 1)
end = dt.datetime.today()
start

8.Set the index to a DatetimeIndex type

df.index = pd.to_datetime(df.index)
type(df.index)

9.Change the frequency to monthly, sum the values and assign it to monthly.

monthly = called.resample('M').sum()
monthly

10.now we have the monthly data. Now change the frequency to year.

year = monthly.resample('AS-JAN').sum()
year




你可能感兴趣的:(pandas中关于时间的处理总结)