Functions

Date

MINUTE

# How to Use Excel's MINUTE Function in Pandas

Excel's MINUTE function extracts the minute from a time value.

This page explains how to implement Excel's MINUTE function in Python using pandas.

### Use Mito's MINUTE function

Mito is an open source library that lets you write Excel formulas in Python. Either write the formula directly in Python or use the MINUTE formula in the Mito Spreadsheet and generate the equivalent Python code automatically.

Mito's MINUTE function works exactly like it does in Excel. That means you don't need worry about managing data types, handling errors, or the edge case differences between Excel and Python formulas.

Install Mito to start using Excel formulas in Python.

```
# Import the mitosheet Excel functions
from mitosheet.public.v3 import *;
# Use Mito's MINUTE function
# Note: No need to convert the column to a datetime first
# because Mito's MINUTE function handles that automatically
df['Minute'] = MINUTE(df['Date'])
```

## Implementing the Minute Extraction function in Pandas#

Recreating Excel's MINUTE function behavior in Python requires a combination of pandas operations. Here are some common implementations:

### Extracting Minute from Datetime#

In Excel, if you have a datetime value, you can use the MINUTE function to return the minute component. Similarly, in pandas, you can use the `.dt` accessor followed by the `minute` attribute.

For example, in Excel you might use =MINUTE(A2). In pandas:

`df['Minute'] = df['Datetime_Column'].dt.minute`

### Converting string to datetime and then extracting the minute#

Often, Pandas will infer the data type of your column as string, even if the data to you looks like a date, ie: 1/2/23 12:30:00. In these cases, you need to convert the string to datetime before extracting the minute.

To do this in pandas, first use `pd.to_datetime` to convert the column to a datetime column, and then extract the minute:

```
# Convert the string to datetime
df['Datetime_Column'] = pd.to_datetime(df['String_Column'])
# Extract the minute from the datetime column
df['Minute'] = df['Datetime_Column'].dt.minute
```

### Grouping Data by Minute#

There are situations where you want to aggregate data based on minute. In Excel, you might use a pivot table after extracting the minute. Similarly, in pandas, after extracting the minute, you can use the `groupby` method

For example, if you have a column called 'Date' and a column called 'Website Traffic', you might want to group the data by minute and sum the traffic for each minute.

```
df['Minute'] = df['Date'].dt.minute
grouped_data = df.groupby('Minute').agg({'Website Traffic': 'sum'}).reset_index()
```

## Common mistakes when using MINUTE in Python#

While implementing the MINUTE function equivalent in pandas, a few common pitfalls might occur. Here's how to navigate them.

### Incorrect datatypes#

The `.dt` accessor is exclusive to pandas Series with datetime64 data types. Using it on non-datetime columns will raise an AttributeError.

For example, if you have a column called 'Date', but it actually has an object data type, you'll need to convert it to datetime before using the `.dt` accessor. You can check the data type of a column using `df.dtypes`.

```
# Ensure the column is of datetime dtype
df['Datetime_Column'] = pd.to_datetime(df['Datetime_Column'])
df['Minute'] = df['Datetime_Column'].dt.minute
```

### Forgetting to Handle Null Values#

If your dataset has missing or NaT (Not-a-Timestamp) values in the datetime column, trying to extract the minute from them will result in NaN (Not a Number) values. Make sure to handle or filter them out as necessary.

```
# Drop rows with NaT values before extracting minute
df.dropna(subset=['Datetime_Column'], inplace=True)
df['Minute'] = df['Datetime_Column'].dt.minute
```

## Understanding the Minute Extraction Formula in Excel#

The MINUTE function in Excel returns the minute of a time value, ranging from 0 to 59.

=MINUTE(serial_number)

### MINUTE Excel Syntax

Parameter | Description | Data Type |
---|---|---|

serial_number | The time value from which you want to extract the minute. | A valid Excel time |

### Examples

Formula | Description | Result |
---|---|---|

=MINUTE("5/21/2021 9:30 PM") | Extracts the minute from the given time. | 0 |

=MINUTE("21-May-2021 6:30 AM") | Extracts the minute from the given time. | 30 |

=MINUTE("5/21/2021 9:59 PM") | Extracts the minute from the given time. | 59 |

**Don't re-invent the wheel. Use Excel formulas in Python.**

## Don't want to re-implement Excel's functionality in Python?

## Edit a spreadsheet.

Generate Python.

Mito is the easiest way to write Excel formulas in Python. Every edit you make in the Mito spreadsheet is automatically converted to Python code.

View all 100+ transformations →