Files
growth_report/utils/zodiac_utils.py

31 lines
848 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
def calculate_zodiac(birth_date):
"""
根据出生日期推断生肖 (基于公历年份简单算法)
逻辑1900年是鼠年以此为基准进行模运算
"""
if pd.isna(birth_date):
return "未知"
# 确保是 datetime 对象,方便提取年份
try:
birth_date = pd.to_datetime(birth_date)
except:
return "格式错误"
year = birth_date.year
# 生肖列表,顺序必须固定:鼠、牛、虎...
zodiacs = ['', '', '', '', '', '', '', '', '', '', '', '']
# 算法核心:(年份 - 1900) % 12 得到索引
# 1900年是鼠年索引为0
index = (year - 1900) % 12
return zodiacs[index]
# --- 主程序 ---
# 1. 读取 Excel 文件
file_path = 'names.xlsx' # 你的文件名