pd.to_datatime format
时间: 2024-04-15 13:27:00 浏览: 101
boost_date_time-vc120-mt-1_56.dll
The `pd.to_datetime()` function in pandas is used to convert a given argument to a datetime object. It can handle a variety of input formats, and you can specify the format using the `format` parameter. Here are some examples of format codes commonly used:
- `%Y` - 4-digit year
- `%m` - 2-digit month (01 to 12)
- `%d` - 2-digit day (01 to 31)
- `%H` - 2-digit hour (00 to 23)
- `%M` - 2-digit minute (00 to 59)
- `%S` - 2-digit second (00 to 59)
You can combine these format codes with delimiters like `-`, `/`, `:`, etc., to match your input string's format. For example, if your input string is in the format "YYYY-MM-DD", you can use the format code `"%Y-%m-%d"`.
Here's an example usage:
```python
import pandas as pd
date_string = "2022-01-01"
date_object = pd.to_datetime(date_string, format="%Y-%m-%d")
print(date_object)
```
This will output: `2022-01-01 00:00:00`, which is a datetime object representing the given date.
阅读全文