📜  从 Pandas-Python 中的时间戳获取分钟

📅  最后修改于: 2022-05-13 01:55:11.027000             🧑  作者: Mango

从 Pandas-Python 中的时间戳获取分钟

Pandas 是一个为Python语言构建的开源库。它提供了各种数据结构和操作来操作数值数据和时间序列。

在这里,让我们使用 pandas 提供的一些方法从时间戳中提取分钟的值。

方法一:使用pandas.Timestamp.minute属性。

pandas 的这个属性可用于从给定的时间戳对象中提取分钟。

示例 1:
让我们首先在下面创建一个时间戳对象:

Python3
# import pandas library
import pandas as pd 
    
# create a Timestamp object 
time_stamp = pd.Timestamp(2020, 7, 20,
                          12, 41, 32, 15) 
    
# view the created time_stamp
print(time_stamp)


Python3
# display the value of minute from
# the created timestamp object
print(time_stamp.minute)


Python3
# import pandas library
import pandas as pd 
    
# create a Timestamp object 
time_stamp = pd.Timestamp(2020, 7, 20) 
    
# view the created time_stamp
print(time_stamp)


Python3
# display the value of minute from
# the created timestamp object
print(time_stamp.minute)


Python3
# import pandas library
import pandas as pd 
  
# create a series
sr = pd.Series(['2020-7-20 12:41', 
                '2020-7-20 12:42', 
               '2020-7-20 12:43',
                '2020-7-20 12:44'])
  
# convert the series to datetime
sr = pd.to_datetime(sr)
  
# create a pandas dataframe with a
# column having timestamps
df = pd.DataFrame(dict(time_stamps = sr))
    
# view the created dataframe
print(df)


Python3
# extract minutes from time stamps and
# add them as a separate column
df['minutes_from_timestamps'] = df['time_stamps'].dt.minute
  
# view the updated dataframe
print(df)


Python3
# import pandas library
import pandas as pd 
  
# create a series
sr = pd.Series(pd.date_range('2020-7-20 12:41', 
                             periods = 5,
                             freq = 'min'))
  
# create a pandas dataframe with a
# column having timestamps
df = pd.DataFrame(dict(time_stamps=sr))
    
# view the created dataframe
print(df)


Python3
# extract minutes from time stamps and
# add them as a separate column
df['minutes_from_timestamps'] = df['time_stamps'].dt.minute
  
# view the updated dataframe
print(df)


输出:

时间戳对象

在上面创建的时间戳对象中,分钟的值为“41”。让我们使用Timestamp.minute属性提取此值。

Python3

# display the value of minute from
# the created timestamp object
print(time_stamp.minute)

输出:

分钟

示例 2:

创建时间戳对象:

Python3

# import pandas library
import pandas as pd 
    
# create a Timestamp object 
time_stamp = pd.Timestamp(2020, 7, 20) 
    
# view the created time_stamp
print(time_stamp)

输出:

时间戳对象

在上面创建的时间戳对象中,分钟的值为“0”。让我们使用Timestamp.minute属性提取这个值

Python3

# display the value of minute from
# the created timestamp object
print(time_stamp.minute)

输出:

分钟

方法 2:使用Series.dt.minute 属性。

现在,考虑具有包含时间戳的列之一的 pandas 数据框的示例。在这种情况下,我们将首先使用Series.dt方法以 DateTime 对象的形式访问系列的值,然后使用minute属性从 datetimes 对象中提取分钟。

示例 1:
首先,创建一个熊猫数据框:

Python3

# import pandas library
import pandas as pd 
  
# create a series
sr = pd.Series(['2020-7-20 12:41', 
                '2020-7-20 12:42', 
               '2020-7-20 12:43',
                '2020-7-20 12:44'])
  
# convert the series to datetime
sr = pd.to_datetime(sr)
  
# create a pandas dataframe with a
# column having timestamps
df = pd.DataFrame(dict(time_stamps = sr))
    
# view the created dataframe
print(df)

输出:

时间戳对象

从数据框中的每个时间戳中提取分钟:

Python3

# extract minutes from time stamps and
# add them as a separate column
df['minutes_from_timestamps'] = df['time_stamps'].dt.minute
  
# view the updated dataframe
print(df)

输出:

分钟

示例 2:

创建一个熊猫数据框:

Python3

# import pandas library
import pandas as pd 
  
# create a series
sr = pd.Series(pd.date_range('2020-7-20 12:41', 
                             periods = 5,
                             freq = 'min'))
  
# create a pandas dataframe with a
# column having timestamps
df = pd.DataFrame(dict(time_stamps=sr))
    
# view the created dataframe
print(df)

输出:

时间戳对象

从数据框中的每个时间戳中提取分钟:

Python3

# extract minutes from time stamps and
# add them as a separate column
df['minutes_from_timestamps'] = df['time_stamps'].dt.minute
  
# view the updated dataframe
print(df)

输出:

分钟