728x90
In [31]:
import pandas as pd
import datetime as dt
In [33]:
pd.Timestamp('2023, 3, 30')
Out[33]:
Timestamp('2023-03-30 00:00:00')
In [34]:
# Pandas Timestamp
pd.Timestamp(dt.datetime(2022, 3, 31, 8, 0, 15,))
Out[34]:
Timestamp('2022-03-31 08:00:15')
In [35]:
# Difference between two dates
day_1 = pd.Timestamp('1990, 3, 31, 11')
day_2 = pd.Timestamp('2022, 3, 31, 11')
delta = day_2 - day_1
print(delta)
11688 days 00:00:00
In [36]:
date_1 = dt.date(2022, 3, 31)
date_2 = dt.date(2022, 4, 30)
date_3 = dt.date(2022, 5, 31)
In [37]:
# date time index
dates_list = [date_1, date_2, date_3]
dates_index = pd.DatetimeIndex(dates_list)
dates_index
Out[37]:
DatetimeIndex(['2022-03-31', '2022-04-30', '2022-05-31'], dtype='datetime64[ns]', freq=None)
In [39]:
# store sales
sales = [50, 35, 55]
Out[39]:
2022-03-31 50
2022-04-30 35
2022-05-31 55
dtype: int64
In [40]:
sales = pd.Series(data = sales, index = dates_index)
sales
Out[40]:
2022-03-31 50
2022-04-30 35
2022-05-31 55
dtype: int64
In [42]:
# A range of dates
my_days = pd.date_range(start = '2022-01-01', end = '2022-04-01', freq='D')
my_days
Out[42]:
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12',
'2022-01-13', '2022-01-14', '2022-01-15', '2022-01-16',
'2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20',
'2022-01-21', '2022-01-22', '2022-01-23', '2022-01-24',
'2022-01-25', '2022-01-26', '2022-01-27', '2022-01-28',
'2022-01-29', '2022-01-30', '2022-01-31', '2022-02-01',
'2022-02-02', '2022-02-03', '2022-02-04', '2022-02-05',
'2022-02-06', '2022-02-07', '2022-02-08', '2022-02-09',
'2022-02-10', '2022-02-11', '2022-02-12', '2022-02-13',
'2022-02-14', '2022-02-15', '2022-02-16', '2022-02-17',
'2022-02-18', '2022-02-19', '2022-02-20', '2022-02-21',
'2022-02-22', '2022-02-23', '2022-02-24', '2022-02-25',
'2022-02-26', '2022-02-27', '2022-02-28', '2022-03-01',
'2022-03-02', '2022-03-03', '2022-03-04', '2022-03-05',
'2022-03-06', '2022-03-07', '2022-03-08', '2022-03-09',
'2022-03-10', '2022-03-11', '2022-03-12', '2022-03-13',
'2022-03-14', '2022-03-15', '2022-03-16', '2022-03-17',
'2022-03-18', '2022-03-19', '2022-03-20', '2022-03-21',
'2022-03-22', '2022-03-23', '2022-03-24', '2022-03-25',
'2022-03-26', '2022-03-27', '2022-03-28', '2022-03-29',
'2022-03-30', '2022-03-31', '2022-04-01'],
dtype='datetime64[ns]', freq='D')
In [43]:
type(my_days) # 판다스에 쓰일 수 있는 인덱스
Out[43]:
pandas.core.indexes.datetimes.DatetimeIndex
In [44]:
type(my_days[4])
Out[44]:
pandas._libs.tslibs.timestamps.Timestamp
In [47]:
# a range of dates
my_days = pd.date_range(start = '2022-01-01', end = '2022-08-01', freq='M')
my_days
Out[47]:
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
'2022-05-31', '2022-06-30', '2022-07-31'],
dtype='datetime64[ns]', freq='M')
In [48]:
# 시작 날짜를 기점으로 이후 20일에 대한 샘플
pd.date_range(start = '2022-01-01', periods = 20, freq="D")
Out[48]:
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10', '2022-01-11', '2022-01-12',
'2022-01-13', '2022-01-14', '2022-01-15', '2022-01-16',
'2022-01-17', '2022-01-18', '2022-01-19', '2022-01-20'],
dtype='datetime64[ns]', freq='D')
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
[datetime]S7_04_Practical_example2 (0) | 2023.01.21 |
---|---|
[datetime]S7_03_Practical_example1 (0) | 2023.01.21 |
[datetime]S7_01_datetime (0) | 2023.01.21 |
[seaborn]S6_02_pairplot,displot,heatmap(correlations) (0) | 2023.01.21 |
[seaborn]S6_01_scatter&count_plot (0) | 2023.01.21 |
댓글